aspose file tools*
The moose likes Servlets and the fly likes allocating a thread to a servlet request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "allocating a thread to a servlet request " Watch "allocating a thread to a servlet request " New topic
Author

allocating a thread to a servlet request

Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

In Head First Servlets and JSP it is emphasized again and again that ...
the container starts a new thread or allocates a thread from the pool


is their any difference between the two ... ???

if it is ... then does the pool contains a limited number of threads or so ???

moreover, as after the service method completes the thread is destroyed and the request and response objects are eligible for garbage collection ... then doesn't it effects the number of threads in the pool ???

i personally think ... it's just the way to teach saying these two things but just want's to clarify ... hope no one minds


http://plainoldjavaobject.blogspot.in
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3177
    
  47

Lalit Mehra wrote:moreover, as after the service method completes the thread is destroyed and the request and response objects are eligible for garbage collection ... then doesn't it effects the number of threads in the pool ???

Why do you think it can affect the thread?
A thread which has completed the life-cycle of a servlet can be detached from that servlet and pooled for a later execution on another servlet. The way how it is done is up to the container provider and it's not defined in the JavaEE specification. A book, which is generically targeting on the Servlet and JSP technologies cannot explain you the underneath techniques of various container providers - there are many.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
as after the service method completes the thread is destroyed and the request and response objects are eligible for garbage collection ..


No - thats the whole point of Thread pools, the Thread is available for the next request. HttpRequest and response objects may also be managed in a pool - that is up to the implementor. A google search for Java Thread Pool Implementation turned up lots of examples you could study.

Bill
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

ok ... so that means it is not completely destroyed and can be allocated to some other servlet request if need be ?
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Yep. That is why the term 'pool' is used. As said above the underlying implementation varies and these threads are supported by operating system threads which in turn work on hardware threads (if present).


SCJP, SCWCD.
|Asking Good Questions|
srinu polam
Greenhorn

Joined: Jul 12, 2010
Posts: 2

container creates an instance of Thread class for each request.I think it willnt maintain a pool of threads
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
srinu polam wrote:container creates an instance of Thread class for each request.I think it willnt maintain a pool of threads

As was mentioned before, that is up to the servlet container. But object creation in general has become much cheaper over the years as JVMs have become smarter, and pooling of objects for reuse has consequently become less important (network connections -like to a DB- are an exception to this). So it's possible that modern containers no longer use thread pools.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: allocating a thread to a servlet request