On Tue, 23 May 2000, 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 ,

Here is my understanding of this: It is not true that most servlet
engines will create multiple instances of a servlet.  Prior to Servlet
Spec 2.2, generally the only case where a servlet engine would create
multiple instances of a servlet is when that servlet implemented
SingleThreadModel (which apparently is not the case here).  Servlet
Spec 2.2 *mandated* that that was the only circumstance where serlvet
containers (the new name for serlvet engines) could create multiple
instances of a servlet.  So if you are not implementing
SingleThreadModel (and many think it is a bad idea to do so; cf. past
discussion on this list), and you are using a servlet container that
implements 2.2, you can be sure that only one instance of a servlet
will be created.

(There's one thing I'm having problem resolving with this: I believe
that you can have several aliases for the same servlet, perhaps
varying by the init arguments they take.  If that is the case, it
would seem that each alias would require a separate instance of the
servlet.  Can anyone clarify this issue?)

> my personal view is that it is better to protect the object you care
> about, so in this case it would be the FileOutputStream.

To clarify, I believe what Tim is saying here is that if you are going
to synchronize at all, you should synchronize on as specific an
object, and as small a level (code-wise) as possible -- that is better
than synchronizing the entire method because it limits the scope and
duration of the lock.

Also, Craig McClanahan had a followup which indicated that if you are
only writing to a file (i.e. not reading from it), such as a log, you
do not need to synchronize at all as the OS will (should?) handle it
properly.

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

Reply via email to