> 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).

Well, I would say: For every REQUEST (not SESSION as you stated) which call
the servlet, the servlet container will create a thread for that servlet (in
fact, for his service method). It is true that all the threads will use the
same instance of the servlet (the same methods and the same instance
variables). As a consequence every access (in write)  from the servlets
methods (except init) to instance variables should be synchronized and
moreover every access (in write) to shared resources (such as a file) should
be synchronized.

If your servlet implements SingleThreadedModel interface there will be
exactly one request thread at a time for the service method but the access
to the external shared resources  should be synchronized. However, I don't
recommend using  SingleThreadedModel as there are performance penalties and
still there are other threading issues to deal with.

>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.

As you see, you are right!

Best regards

Marius Barduta
Software Developer,
NECOMM, Iasi, Romania

___________________________________________________________________________
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