Hi,
On Tue, 9 Mar 1999, sanjuktas wrote:
> while synchronizing a thread why is it necessary to synchronize on a obj
> when it is already inside a synchronized method ?
Cause there might be different operations, wich apply to different objects:
- this - an instance of classExample wich you call
its process() method.
- some other object (you refer it as "the class lock"; I suppose
is a lock on a java.lang.Class instance, or to a static referenced
object..) .
Your code, with comments:
public class ClassExample {
// This variable is used to synchronise at class level;
// it is shared among ALL instances of ClassExample:
static Object classLock = new Object();
synchronise void process() {
// make some internal object processing. No other thread will be able
// to get synchronised access on this **same** instance of ClassExample.
// That means you can have classExampleInstance1.process() and
// classExampleInstance2.process() work in parallel!
synchronised(classLock) {
// Here you want to be sure that no other thread will be allowed
// to execute same or other
// code synchronised(classLock) { } in ANY of classExample's
// instances ...
}
}
}
>
> Iis it not that it belongs to the synchronized block and thus have mutual
> exclusion. ?
No. Mutual exclusion will not occur within the same processing thread;
you may have many locks on the same object as long as you stay within the
same thread..
Cezar Totth email: [EMAIL PROTECTED]
Fax: (401) 220 33 95
Genesys Software Romania Phone: (401) 638 49 44
Stefan Furtuna 169, sect.6
cod 77171, Bucharest
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