Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat threads vs Java threads

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?

like image 567
black666 Avatar asked May 11 '10 11:05

black666


People also ask

What is a Tomcat thread?

Threads determine the maximum number of requests the Tomcat server can handle simultaneously. Since the number of available threads directly affects how efficiently Tomcat can process requests, monitoring thread usage is important for understanding request throughput and processing times for the server.

Is Tomcat server multithreaded?

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.

How many threads can Tomcat handle?

The default installation of Tomcat sets the maximum number of HTTP servicing threads at 200. Effectively, this means that the system can handle a maximum of 200 simultaneous HTTP requests.

Is Tomcat single threaded?

By default, Tomcat allocates a single thread to perform deployment and management of applications. When multiple applications are handled by a single Tomcat instance, this can cause the startup time to increase considerably, as each application is started in sequence by the management thread.


3 Answers

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.

like image 50
skaffman Avatar answered Sep 24 '22 08:09

skaffman


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).

like image 43
matt b Avatar answered Sep 25 '22 08:09

matt b


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.

like image 40
Adam Gent Avatar answered Sep 23 '22 08:09

Adam Gent