3

In the Tomcat container, do all the filters associated to a servlet and the servlet itself use the same thread? i.e, will doFilter() be run in the same thread as a servlet's service() method? Thanks in advance.

3 Answers 3

6

I suspect the question is more: Can I use ThreadLocal objects to pass data between filters and to servlets? In which case the answer is, absolutely. App servers do this themselves to track security, transaction, connection and other request information.

Just make sure you clean out your ThreadLocal in a finally block in the same code that sets the ThreadLocal.

As already noted by other, the servlets themselves may or may not be synchronized, but that is orthogonal to the concept of thread state; i.e. how many threads execute against object foo vs. can I put state in the thread and have it seen by object foo. The answer to the second question is always yes.

The only time the answer would be 'no' is if you made use of any asynchronous communication:

  • Dispatched the call via AsyncContext
  • mixed in EJBs and started making use of @Asynchronous or the TimerService

These involve the app server starting new threads not associated with the original request thread so any ThreadLocal state will not travel with the new thread. It is also why those APIs do not allow security and transaction context of the caller to be propagated to the method invoked as the caller and method are in different threads.

Advanced note, InheritableThreadLocal typically does not work as async calls are typically done by the server against a thread pool rather than creating child threads of the caller thread.

4

Yes, each request is performed in a single thread, including all filters and the target servlet.

-1

Yeah, each request is performed within a single servlet instance.Servlet container receives each request and start a new thread which contains the HttpServletRequest and HttpServletResponse. This thread process the request in the service method of the servlet instance and will be destroyed as soon as the service method completes.

6
  • The servlet container manages the servlet lifecycle, not the developer.
    – duffymo
    Aug 2, 2010 at 9:27
  • 1
    It is not true that the service method is synchronized - unless the servlet implements the SingleThreadModel interface, Tomcat will happily run threads in parallel servicing the same servlet.
    – nos
    Aug 2, 2010 at 9:35
  • 1
    Note that SingleThreadModel is deprecated since Servlet 2.4 over 5 years ago. You should not make use of it. It would indicate a bad design.
    – BalusC
    Aug 2, 2010 at 11:14
  • 1
    Note that Tomcat and most app servers do not new/destroy threads per request as described above, but rather pool threads and reuse them. So be sure to clean out any ThreadLocal state you may set or it will come back in another request later. Aug 2, 2010 at 19:07
  • Thanks a lot for the answers. I have been trying to find these specifications in the Tomcat documentation but I have not been able. Does anyone know how to find it?
    – rvelaz
    Aug 3, 2010 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.