You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Takumi Fujiwara <tr...@yahoo.com> on 2002/08/27 18:13:55 UTC

Threads Question in Tomcat

Hi,

I have a few questions regarding Threads in Tomcat. I
appreciate if anyone can help me on these:
1. Is it true that requests from the SAME client
always served by the SAME thread? 
2. What if the SAME client access 2 different servlets
in a servlet container? Is it still served by the SAME
thread?
3. What happens if the client send another request
BEFORE the first request finished execution? Will the
second request queue up somewhere in servlet
container? Or it will be served by a different thread?
4. Is it recommend to use ThreadLocal when I write
Servlet code?

Thanks a lot for your help.
Sam

__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Threads Question in Tomcat

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 27 Aug 2002, Takumi Fujiwara wrote:

> Date: Tue, 27 Aug 2002 09:13:55 -0700 (PDT)
> From: Takumi Fujiwara <tr...@yahoo.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: tomcat-user@jakarta.apache.org
> Subject: Threads Question in Tomcat
>
> Hi,
>
> I have a few questions regarding Threads in Tomcat. I
> appreciate if anyone can help me on these:

> 1. Is it true that requests from the SAME client
> always served by the SAME thread?

No.  The set of threads Tomcat uses for requests are shared across *all*
users for *all* web applications that are installed.

> 2. What if the SAME client access 2 different servlets
> in a servlet container? Is it still served by the SAME
> thread?

Only if this happens on the same request (i.e. you used
RequestDispatcher.include() or RequestDispatcher.forward(), or the
equivalent JSP tags <jsp:include/> or <jsp:forward/>.  Otherwise, which
thread gets used for which request is totally arbitrary.

> 3. What happens if the client send another request
> BEFORE the first request finished execution? Will the
> second request queue up somewhere in servlet
> container?

No, unless all the configured request processing threads are currently
busy.  This is set in the <Connector> element in server.xml.

> Or it will be served by a different thread?

Yes.  This is pretty common, for example, in UIs that use frames -- the
browser will often submit multiple simultaneous requests to load the
various frames.  Each of them will appear to the server as a separate
request, handled by a separate thread.

> 4. Is it recommend to use ThreadLocal when I write
> Servlet code?

You can use ThreadLocal, but only in the context of a particular call to
the service() method of a servlet.  Therefore, the valid lifetime for
objects stored there is basically the same as a local variable in the
service() method itself, which has a lot less overhead.  So, there is
little or nothing to be gained by using ThreadLocal.

The typical pattern for per-request state information is to store those
objects as request attributes instead of messing with ThreadLocal.  Stuff
you need across requests from the same user can be stored as session
attributes instead.

>
> Thanks a lot for your help.
> Sam

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>