[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: servlets are multithreaded, right?
The interviewer is technically correct, the servlet it not multithreaded
since servlets do not normally implement Runnable or extend Thread. The
containers are normally multithreaded, (Servlet 2.3 p 133 and 185). So
there can be multiple threads accessing the same instance of a servlet
even though the servlet itself is not multithreaded.
I could not find a reference saying that servlets could not implement
Runnable and manage their own threads. Session Beans on the other hand
are explicitally prohibited from doing this.
SingleThreadedMode only prohibits multiple threads from accessing an
given instance of a servlet. The Spec does not prevent the container
from creating multiple instances of the servlet to service multiple
threads.
I probably would have missed this one on the interview too. Most people
say, "Servlets are multithreaded". And they do behave as such. But
they do not implement Runnable or extend Thread.
On Tue, 2003-10-28 at 21:17, ktm@speakeasy.net wrote:
> > -----Original Message-----
> > From: Chris Abney [mailto:chrisabney@charter.net]
> > Sent: Tuesday, October 28, 2003 11:54 PM
> > To: ajug-members@ajug.org
> > Subject: Re: servlets are multithreaded, right?
> >
> > I believe this is how it goes:
> > The servlet itself is basically passive.
> > The web server spawns new threads and that calls servlet class methods.
> > So you can say - Tomcat, Jetty, ServletRunner, or whatever is the multi-threaded
> > part of the architecture.
> > However, it is always the programmer writing servlet code that has to deal with
> > concurrency issues.
>
> You are making sense. The servlet developer is concerned with reentrant vs non-reentrant code. I'm not up to date on the spec, but I don't recall any requirements which state that the servlet container is or is not multi-threaded. The requirement is that the container guarantees SingleThreadedModel servlets are never entered by more than 1 thread at a time. Has the servlet spec changed here?
>
> A better question to ask on an interview is :
> When is it appropriate to use the SingleThreadedModel interface?
>
> Kit
>
>
--
Adversity gives birth to greatness. The greater the challenges and
difficulties we face, the greater opportunity we have to grow and
develop as people. A life without adversity, a life of ease and comfort,
produces nothing and leaves us with nothing. This is one of the
indisputable facts of life.
SGI President Ikeda's Daily Encouragement for April 25