What if a synchronized method calls another synchronized method (in another class), which does wait(). Will the lock be released in the first synchronized method as well, although being in another class?
E.g.
public class A {
private B b;
public A(B b) {
this.b = b;
}
public synchronized String a() {
return b.b();
}
}
public class B {
public synchronized String b() {
while (!someCondition) wait();
return "Success!";
}
}
So my question is, will it be possible to enter another synchronized method in A during the time someCondition = false? Or does wait() just make it possible to enter other synchronized methods in B while an attempt to enter another synchronized method in A will fail until a() returns?
No. While invoking
aand subsequentlyb, no other method can enter neitheraorb(or any other synchronized method of classesAorB).b.wait()however releases the lock on all synchronized methods onBsinceis essentially equivalent to