Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Action class object pool?

 
Amol H Lekurwale
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I read from a site that containers like tomcat create a pool of objects for servlet. This they state that is an example of multithreading.
Can anyone explain me what has a pool of objects to do with multithreading? If I dont declare any global variable inside a servlet, and as each thread gets its own copy of local variables, why cant a single object handle all request?
Now, as containers do it with servlet, what is done with struts action class object? Is it that a single action object takes care of all requests or a pool is created of objects for the action class? If a pool is created, where do we configure the pool size in a web-app?

Regards.
Amol
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Struts 2, a single instance of the Action handles one request and that instance is not reused to serve other requests. I'm not sure about Struts 1.x...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts 1 actions are like servlets; there is only one created per application.
 
Amol H Lekurwale
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
Does this mean that if more than one request is arriving at the same time or when the first one is being processed, the other has to wait? Or a new instance is created to serve each next request? Also, wouldn't this approach cause a performance issue?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amol H Lekurwale wrote:Does this mean that if more than one request is arriving at the same time or when the first one is being processed, the other has to wait?

No, the container calls the appropriate instance method in a new thread.
Or a new instance is created to serve each next request?

No, not in Struts 1. In Struts 2, as previously mentioned, a new instance is created for each request.
Also, wouldn't this approach cause a performance issue?

No: object creation in Java is *very* fast. If you're asking about when no new instance is created, also no; multiple threads can call the same method in a single instance of a class.
 
Amol H Lekurwale
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
Thanks for your quick response.
One last query : For Webcontainers like Tomcat, JBoss etc. : If my servlet is *not* implementing SingleThreadModel interface, would that mean only a *single* instance of that servlet is created, always? In case by any means, if we are able to specify the pool size in such a case, then how many instances will be created? Single or as many defined in the configurations.

Regards,
Amol
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know there will only ever be one instance of a servlet created, but I could be wrong.

SingleThreadModel has more to do with the underlying threading than with instantiation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic