On Tue, 23 May 2000, Craig R. McClanahan wrote:
> Tim Panton - Westhawk Ltd wrote:
>
> > Marc Krisjanous <[EMAIL PROTECTED]> wrote:
> > __________
> > >Hi all,
> > >I wonder if anyone can confirm the following:
> > >
> > >I have a servlet that will call a synchronized method that will
> > >write to a file. My understanding of how Servlets work is that
> > >for every web session (web browser) which calls the servlet the
> > >servlet will have a thread created for that session in which data
> > >will be stored for that session but each thread will use the same
> > >method(e.g the class and methods will not be created for each
> > >thread). Thus in order to access and write to external media you
> > >need to make that method synchronized since in theory there is
> > >only one method that all threads share.
> > >
> > >Is this true??
> >
> > no, since the servlet engine may make multiple instances of your
> > sevlet (most will), and Java syncronizes on an object. In any case
> > , my personal view is that it is better to protect the object you
> > care about, so in this case it would be the FileOutputStream.
>
> Threads are created by the servlet container per *request*, not per
> *session*. It is quite possible for multiple requests to come in
> and access the same session at the same time -- consider a framed
> presentation where you are updating multiple frames at once (and
> your browser submits multiple requests at the same time), or a user
> who starts a long request, presses STOP, and then starts another
> request.
I think there is an unfortunate confusion of terminology/language
here. As Craig points out, "request" and "session" have
specific/strict meanings in the context of the servlet API. But I
think the original poster used "session" less strictly, actually more
for what "request" in the strict sense is normally used. So I'm not
sure that this part of Craig's response is relevant, and hence may
only serve to confuse things.
> The general advice of synchronizing on the object you are trying to
> protect is useful -- you want to minimize the amount of time a
> request spends waiting for a lock, and one way to help this is to
> only synchronize modifications to a particular object on that object
> itself. Note that, in the specific case of output to a file from
> multiple threads, most operating systems take care of this for you
> themselves, and don't have problems interleaving the output from the
> various threads.
I think this part of Craig's response hits the nail on the head.
Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html