[英]Calling a synchronized method from another synchronized method, both on different object
[英]Synchronized method calling another synchronized method which calls wait()
如果一個synchronized方法調用另一個同步方法(在另一個類中),它執行wait()會怎么樣。 鎖是否會在第一個同步方法中釋放,盡管在另一個類中?
例如
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!";
}
}
所以我的問題是,在someCondition = false期間,是否可以在A中輸入另一個同步方法? 或者wait()是否可以在B中輸入其他同步方法,而嘗試在A中輸入另一個同步方法將失敗直到a()返回?
在調用a
和隨后的b
,沒有其他方法既不能輸入a
或b
(或A
類或B
類的任何其他同步方法)。 但是b.wait()
會釋放對B
上所有同步方法的鎖定
public synchronized String b()
基本上相當於
public String b() {
synchronized(this) {
..
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.