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??
> >
> >Best Regards
> >
> 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.

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.

>
> Tim.
>

Craig McClanahan

___________________________________________________________________________
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

Reply via email to