On Thu, 5 Oct 2000, Tiana Zhang wrote:

> Well, I think I have to put static keyword to specify my method. If
> not, different instances will be created and synchronize won't
> protect all the instances. Am I understand it correct?

Probably not.  The static keyword indicates that a method (or
variable) is a class method (or variable), i.e. it belongs to the
class, and not any particular instance.  Whether or not this is the
correct thing to do depends on exactly what you are trying to do.

As to your previous message: The synchronized keyword is used to
restrict/protect (or "lock") a block of code so that it is only run by
one thread at a time, based upon some object, i.e. the locking is
based upon some object.  So in general it can be used to do what you
want to do, but you don't really give enough specifics to determine
whether you're using it properly.

Other factors here are whether your servlet implements
SingleThreadModel (hint: don't), and which servlet spec your servlet
engine/container implements (the latest one specifies that only one
instance of a servlet should be created, unless it implements
SingleThreadModel).


> ____________________Reply Separator____________________
> Subject:    Re: Synchronize method in the servlet
> Author: <[EMAIL PROTECTED]>
> Date:       10/5/00 8:06 AM
>
> It's my understanding that the synchronize keyword does exactly as
> the name implies... it synchronizes usage of that method, so that
> other threads wishing to use the method have to wait until the
> thread currently using it is finished.
>
>
> ----- Original Message -----
> From: "Tiana Zhang" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Thursday, October 05, 2000 7:47 AM
> Subject: Synchronize method in the servlet
>
>
> > Hello,
> >
> > The synchronized method I created in the servlet is going to
> > increment a number in the database and display it in the
> > browser. Because I don't want multiple user update the database at
> > same time, I added synchronized keyword in the method.
> >
> > My question is: does synchronize guarantee that only one thread
> > does the update database? Why after many times of testing multiple
> > browsers, I still got same number on different threads which means
> > access to the synchronized method is not exclusive?
> >
> > many thanks in advance.
> >
> > Tiana
> >
>

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