Tomcat threads vs Java threads

25,064

Solution 1

You do have to make your code thread safe in tomcat. Tomcat will invoke your code (i.e. your servlets) from multiple threads, and if that code is not thread-safe, you'll have problems.

Tomcat's threads are no different to any threads you create yourself.

Solution 2

To add on to what skaffman has mentioned, it might seem like you don't need to think about multi-threading when writing a webapp because the Servlet framework/API is oriented completely around implementing methods (service(), doGet(), doPost(), etc) which are invoked once per HTTP request.

Therefore, in a simple application, you can implement these methods in your servlet and/or JSP or whatever and not think about what happens when multiple threads interact.

But the second you start having shared state between servlets or service methods, then without possibly realizing it you are dealing with multiple threads interacting, and if you aren't careful, you will eventually have multi-threading or synchronization issues. You will have to deal with this because in Tomcat (and I assume all servlet containers, although I don't know if it's required by the Servlet spec) each request is handled by (possibly) a different thread. Therefore if you receive two simultaneous requests, these will be handled by two separate threads concurrently (at the same time).

Solution 3

If you think that Tomcat makes your application thread safe write a Servlet with mutable member variables like a non-concurrent hashmap.

Then have the servlet put things in that hashmap for every request. It won't take long to get a lovely concurrency exception.

This is why in general for singleton-like components you have to be very careful with member variables because they are shared between multiple threads accessing the object.

Now the servlet container create a new transient object for every request (which is what some web app frameworks do) you could put behavior that interacted with the member variables in that transient object and be thread safe.

Share:
25,064
black666
Author by

black666

job: java programmer hobbies: poker, american football, game theory

Updated on May 12, 2020

Comments

  • black666
    black666 almost 4 years

    When using java threads, one has to take care of the basic problems that come with concurrency through synchronization etc.

    AFAIK Tomcat also works with threads to handle its workload. Why is it, that I don't have to think about making my code threadsafe when it is running in Tomcat?

  • pakore
    pakore almost 14 years
    Completing your answer: For example, when you write a servlet you should not have local variables in the servlet class. Why? Because the servlet class is not instantiated for every thread. Tomcat reuses instances for serveral threads, so all your variables should be inside doGet or doPost methods, so they are initializated and allocated in different memory place everytime the method is called.
  • Michael Borgwardt
    Michael Borgwardt almost 14 years
    @pakore: local variables are those inside methods. You mean instance variables.
  • pakore
    pakore almost 14 years
    Yeah, I mean instance variables. Sorry. Thanks for the correction :). I can't edit my comment now :(
  • csviri
    csviri over 12 years
    Is this true? It won't create a new thread but it won't create also a new Instance of the class that implements the servlet? (I tried this in grails,tomcat and it seems there were no problems with instance variables)