tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: Running multiple servlet instances
Date Wed, 02 Feb 2000 21:01:41 GMT
"Clark, Tom" wrote:

> The mechanism would need to cache the instances and select among them when
> each request is   received.  But they don't need to be run one at a time,
> they can be run concurrently.  Each instance is single threaded, but not the
> class as a whole.

The separate instances may run concurrently, but the call to the service()
method in any single instance must be synchronized in order to conform to the
servlet spec requirements reagarding SingleThreadModel.

>   Does Tomcat currently queue up requests when a servlet is busy?  Would
> this also work for a pool of servlets?

Tomcat does not queue up requests by itself, but uses the feature of the
"synchronized" key word to fake this.  The code that actually calls your servlet
method looks sort of like this:

    Servlet instance = <look up the reference to this servlet>;
    if ((this is a SingleThredModel servlet>)  {
        synchronized (instance) {
            instance.service(request, response);
    } else {
        instance.service(request, response);

In other words, for STM servlets the calls are single-threaded because of the
"synchronized" block.  For non-STM servlets, the requests all run concurrently.
In both cases, there is one and only one instance of the servlet, per definition
in the "web.xml" file.

If you implement a cache of instances, you will want to maintain your own queue
of waiting requests.  The issue is that you don't care *which* instance becomes
ready next, so you don't want to queue requests for a particular instance the
way that the "synchronized" block above does it.  In essence, pretend your
servlets are bank tellers, who can serve one customer at a time.  What you want
is a single input queue of waiting customers -- any one of which can be served
by any available teller -- instead of a line per teller.

If you've done multithreaded programming before, this is not hugely difficult,
but it is definitely interesting.

>   Tom


View raw message