[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: servlets are multithreaded, right?



Interesting, the semantics of this. I agree that servlets are (technically)
NOT multi-threaded, but that is of little concern. The BIG concern is that
they RUN in a multi-threaded environment, so you have to be careful with the
use of static data members, etc. (as you should be anyway).

If I were the interviewer and the job was a junior position, I would almost
rather the candidate answer "yes they are multithreaded" because at least he
would presumably know "this is a multithreaded environment, so I better be
careful!".

As for a servlet managing its own (multiple) threads. I routinely have what
I call an initialization servlet that I auto-start at container startup time
(using the <load-on-startup> optional tag in the <servlet> tag in the
web.xml file). I then have that initialization start "background" tasks that
do stuff like cleanup operations on some regular basis, transmit email from
a queue, etc.

I have also kicked off "worker" tasks so the work can complete in the
background (perhaps at a lower priority) while the servlet completes the
response to the user (or redirection to a jsp, depending on your
architecture). So I know servlets can definitely run multiple threads. At
least in Tomcat and a few other containers I have worked with.

Brad


> -----Original Message-----
> From: Rob Rutherford [mailto:rrutherford@dglenn.com]
> Sent: Tuesday, October 28, 2003 11:56 PM
> To: Atlanta Java Users Group
> Subject: 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
>
>
>
>