May i know how are Threads created by web container (New thread is created for every request) different from normal Threads which are created by Extending Thread Class or implementing Runnable Interface. Also how does Web container create Threads,even when Servlet interface or the servlets Extending it doesn't contain any run() method.
-
Depends on the web container being used. – Robby Cornelissen Jun 05 '15 at 02:29
-
@Robby : I am Using Apache Tomcat as my web container. – Kumar Jun 05 '15 at 02:32
-
I'm afraid your best bet would be to either search for Tomcat design documents, or read the Tomcat source code. – Robby Cornelissen Jun 05 '15 at 02:33
-
No difference except the logic inside the run method and it is pooled. – Jafar Ali Jun 05 '15 at 05:59
-
4A servlet container does not create a new thread for eery request. It uses a pool of threads. Threads are taken from the pool to handle a request, and then put back into the pool. Threads are threads are threads. Tomcat threads are no different from any other thread. – JB Nizet Jun 05 '15 at 06:21
2 Answers
All the threads in java are created by Extending Thread Class or implementing Runnable Interface. So web container threads are also created in same way.
You dont see run method inside servlet, that's because servlet code is called inside run method of thread which is created by "main" thread of container. Container abstracts all these details, so that we can focus on writing actual logic server by request instaed of worrying about multiple request management.
Every container has "main" thread, the way we have for our standalone application, or similar to SpringMain in spring.
IF you want to to distinguish between container threads and your threads, you can look at their names and you should find a pattern. You can control nomenclature of threads created by your business logic.

- 3,389
- 3
- 22
- 36
There is no difference between threads except that in case of web container threads are generally taken from thread pool, as Creating a new thread object every time is expensive and time consuming.
(Thread pool is a collection of pre-instantiated, idle threads which stand ready to be given work, usually organized in a queue).

- 1,061
- 1
- 15
- 30