If a thread T1 enters a method m1 by obtaining the class level lock, does this mean another thread T2 cannot run a different method m2 by obtaining the object level lock?
Ans:
|
| No, it doesn't mean that. The "class level lock" is just a regular lock on a different object, namely SomeClass.class. The "object level lock" locks on this. Just to make sure I'm following your understanding of the terminology, you're wondering if m1 and m2 can be run concurrently as they are defined below: public class SomeClass {
public synchronized static void m1() {
//do something
}
public synchronized void m2() {
//do something
}
} And the answer is yes, m1 and m2 can be run concurrently. It is functionally equivalent to this: public class SomeClass {
public static void m1() {
synchronized (SomeClass.class) {
//do something
}
}
public void m2() {
synchronized (this) {
//do something
}
}
} Since they are synchronizing on completely different objects, they are not mutually exclusive. |
No comments:
Post a Comment