javamultithreadingservletsweb-container

Difference between Threads created by web container and Normal Threads?


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.


Solution

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