File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Action class object pool? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Action class object pool?" Watch "Action class object pool?" New topic
Author

Action class object pool?

Amol H Lekurwale
Greenhorn

Joined: Aug 06, 2009
Posts: 9
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Struts 1 actions are like servlets; there is only one created per application.
Amol H Lekurwale
Greenhorn

Joined: Aug 06, 2009
Posts: 9
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 06, 2009
Posts: 9
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

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Action class object pool?