It is not necessary to both synchronize the method and synchronize on an
object.  In fact,

public class classExample {
  synchronized void process () {
    doSomething();
  }
}

is equivalent to:

public class classExample {
  void process () {
    synchronized(this) {
      doSomething();
    }
  }
}

The time you normally want the Object Synchronization is if you are trying
to synchronize on a resource from more than one class, like:

public class classOne {
  protected Object lock;
  protected Object value;

  Object get () {
    synchronized(lock) {
      return value;
    }
  }
}

public class classTwo extends classOne {
  void put (Object value) {
    synchronized(lock) {
      this.value = value;
    }
  }
}

These are obviously contrived examples, but I hope they help.
    (*Chris*)
----- Original Message -----
From: sanjuktas <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, March 09, 1999 12:50 AM


>whilew synchronizing a thread why is it necessary to synchronize on a obj
>when it is already inside a synchronized method ?
>
>Iis it not that it belongs to the synchronized block and thus have mutual
>exclusion. ?
>
>
>ex: public class classEexample{
>
>        synchronized void process(){
>
>            synchronized(the class lock){
>
>
>    }
>
>}
>
>
>Thanks
>
>
>SANJUKTA
>
>___________________________________________________________________________
>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
>

___________________________________________________________________________
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