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

Threads for a servlet

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

Since there exists a single instance for a servlet ...and threads are created according to the request for that servlet...

How many threads (Maximum) are allowed for servlet instance....

what if the no of requests exceed this limit...and a new request comes...

what happens to that latest request....

Regards
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

This is normally a configuration setting for the container. In general are stored ina thread pool and are taken from the pool to serve client requests. In theory it would be possible for all of the threads from the worker thread pool to be accessing the same servlet with no ill effects. Consider the Front Controller pattern, for example.

When the worker threads are all being user, user requests often get held in a queue where they are waiting to be processed but have not yet been processed. This is equivalent to just making the thread sleep till it's turn comes around.

If the queue is also filled then requests just get rejected.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi David,

Thanks for the prompt reply!!!

Tell me whats would be the size of the request queue..Is it dependent upon the server and can the user confgiure...this....

Coming to the Front controller pattern like in struts,
will the action servlet will have one thread or multiple threads for user requests...

I know there exists only one instance...but how about the threads depending upon the user requests


And finally,
all along i thought that for a single servlet instance, we create thread for a request...but this thread would be Gc'ed once it completes the process of servicing the request.There is no way we are going to reuse the thread...
Then how come we have thread pool...Can you tell me where i am going off track about this...

Tx in advance
Regards
Probably need to revisit the servlets concepts...
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
The thing that is confusing you is:
we create thread for a request

In the servlet environment, the servlet engine is responsible for managing the threads that are assigned to requests. In the Tomcat configuration file we find these typical settings for a given connector:

The server starts 25 Threads initially for the request Thread pool, can create as many as 150, if the usage falls to the point where 75 threads are not in use, it will destroy some to recover resources.
Bill
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks William,
So the container creates CERTAIN no of threads for each servlet instance when it is deployed ...

This is pooled together in thread pool...

Is the above inference correct!!!

Tx
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by A Kumar:
Thanks William,
So the container creates CERTAIN no of threads for each servlet instance when it is deployed ...

This is pooled together in thread pool...

Is the above inference correct!!!

Tx


It is not certain number of threads for each servlets.It is the number of threads for requests for all servlets/JSPs.


Groovy
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi Pradeip,

According to Williams post..

In the servlet environment, the servlet engine is responsible for managing the threads that are assigned to requests. In the Tomcat configuration file we find these typical settings for a given connector:

code:

maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

The server starts 25 Threads initially for the request Thread pool, can create as many as 150, if the usage falls to the point where 75 threads are not in use, it will destroy some to recover resources.


And according to your post
It is not certain number of threads for each servlets.It is the number of threads for requests for all servlets/JSPs.



Do you mean that 25 is the min threads for requests for all the servlets/JSPs in a single web application...

So how will the 25 threads limit be decided...?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

The thread count is for the whole Tomcat.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks Pradip!!!

But still i have one doubt...

threads are created when the user sends a request for a particular servlet...(first time - instance is created..later same servlet instance services)

Then how that is pooled..i mean a min limit..
[ May 30, 2006: Message edited by: A Kumar ]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

You can read this article for an sample implementation of thread pools
http://www-128.ibm.com/developerworks/java/library/j-jtp0730.html

Java 5 has a ThreadPoolExecutor can pool threads. I am not sure about the timeout value for the Tomcat threads.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
threads are created when the user sends a request for a particular servlet

Where did you get that idea?

In Tomcat the initial Thread pool with the minimum number of Threads is created immediately on startup. The idea being that when the first request appears, an existing Thread will be assigned.

You can verify this with Tomcat's Management application. I have been watching the Thread pool in my public server regularly. Immediately on restart it had 25 Threads - after a couple of days it was running with 50 threads in the pool.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads for a servlet