tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: limit threads per servlet in Tomcat
Date Fri, 08 Dec 2000 19:51:59 GMT
J Y wrote:

> Hi
> I wish to setup Tomcat to limit threads per servlet, say 20 threads to
> execute in one servlet concurrently. the 21th request would cause the
> Tomecat engine to generate a new servlet instance.

There is no way to do this in Tomcat.

On the other hand, doing this would not help performance at all ... it would actually
make performance slightly worse (because of the additional memory occupancy of the second
instance).  It's also against the servlet spec to do this.

> Is there a way to do it. any comment appriciated.
> Also, what's the performance concerns. is that possible to create a pool of
> servelts?

The standard mechanism for this is to say that your servlet implements the
SingleThreadModel interface.  This tells the servlet container to ensure that only one
request at a time in any one servlet instance -- essentially the call to the service()
method becomes synchronized.

To keep performance from totally suffering when you do this, the servlet container has
the *option* to create a pool of instances for you -- which means that the maximum number
of requests you can process simultaneously is limited to the configured maximum number if
instances in this pool.  Tomcat does *not* support this capability -- effectively the
pool size is one.

Further, IMHO, using SingleThreadModel is a very bad idea.  People like it because it
lets them use instance variables to store per-request state information, instead of using
local variables and passing arguments around (like they should :-).  However,
SingleThreadModel only gives you the illusion of thread safety -- you're still going to
have to deal with simultaneous requests if you are using static variables, or if you are
using sessions.  Why artificially cripple your servlet's performance, and get yourself in

You are far better off *not* implementing SingleThreadModel.  Then, the only limit on how
many simultaneous requests you can support is the maximum number of threads in your
container, with no artificial limits imposed at the individual servlet level.

> Thanks
> Jay

Craig McClanahan

View raw message