On Wed, 28 Aug 2002, Nikola Milutinovic wrote:
> Date: Wed, 28 Aug 2002 07:15:57 +0200
> From: Nikola Milutinovic <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: Tomcat Users List <[EMAIL PROTECTED]>
> Subject: Re: Threads Question in Tomcat
>
> >>1. Is it true that requests from the SAME client
> >>always served by the SAME thread?
> >
> >Not always. Even if a servlet implements SingleThreadModel, the
> >container may still keep a pool of instances of this servlet and issue
> >requests as they come in, as long as no two requests share an instance
> >at the same time (i.e. they'll process one at a time).
>
> This is true in a very particular situation. Basically, a servlet
> container will instantiate ONE servlet class into an object. Each
> request is handled via it's own separate thread. Unless Servlet
> implements SingleThreadModel, all threads that are directed to that
> particular servlet will activate it's "service()" method and run in
> paralel.
This is actually the *normal* case -- not any kind of exception.
> In the oposite case, threads will synchronize on that Servlet.
> If I understand things correctly, it is not the servlet that creates
> threads for each request - it is the particular Connector, which than
> forms the request and forwards it to the appropriate handler, filter or
> a servlet.
>
You are correct in one respect; the servlet creates the necessary threads.
However, you are (potentially) incorrect in another respect -- the
container will ***never*** synchronize on servlet instance whose class
does not implement SingleThreadModel -- the typical pattern is that a
single (nont-STM0 servlet instance is processing multiple requests at the
same time.
If you are using SingleThreadModel (STM) servlets instead, the container
guarantees that no more than one request at a time is active in any given
instance -- it ***may*** use synchronization on servlet intsances to
enforce this, but that is an implementation detail and is not guaranteed.
There is no such thing as a SingleThreadModel filter -- filters must
***always*** be programmed to be thread safe.
> So, the only occasion when the same thread would handle TWO requests,
> regardles of who's the originating client, would be when there is a
> thread-pool and first request finishes and the available thread for the
> other request is the one that handled the first one. This is a game of
> chance. Again, I haven't looked into Tomcat internals, but this is how I
> picture any servlet container would work.
>
It appeares you also haven't looked at the servlet spec :-).
It is not legal (or even technically possible) for a single thread to
process more than one request at the same time. Threads are allocated to
a particular request for the lifetime of that request, and then reused.
> >>2. What if the SAME client access 2 different servlets
> >>in a servlet container? Is it still served by the SAME
> >>thread?
> >
> >Not always.
>
> Hardly ever, IMO, see above.
>
If it does happen to be served by the same thread, that is totally
coincidence and not by design.
> Nix.
>
Craig
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>