Search code examples
javamultithreadingwebserverthread-localweb-container

Inside the Web container how more than 1 object of the same class is getting created/managed which has same reference variable


Probably the stupidest question you have ever heard.

Inside the Web container how more than 1 object of the same class is getting created/managed which has same reference variable... Let me explain with an example.

Inside my controller class I have a code piece

AdminUser adminUser= new AdminUser();

So when 2 Admins signs-in to my web application, there will be 2 Objects of the class AdminUser with same reference variable "adminUser"

  1. How is it possible, is it 2 different threads?
  2. Who is managing this threads, web container?
  3. If so, how web container is doing it, is it wrapping application code with threadLocal?
  4. If its different threads, to maintain a global object (say a counter for the admins visit counts), "static" won't suffice... it needs to be "volatile" instead, correct?

Solution

  • This was the answer that I was looking for. Thanks everyone who answered.

    Does application server create new thread for each request from same user?

    Web Container (Tomcat) spawns new thread for each request (not really, it uses thread pool for performance reasons).

    For any request (doesn't matter who made it) a thread is obtained from the pool, the request is processed then the thread goes back to the pool.