[英]Java Threads wait, notify in one class giving unexpected results
/ 我已經將以下程序編寫為wait()和notify()。 但是在轉到First put()方法之后,線程不再前進。 誰能解釋一下我在以下代碼中所犯的錯誤。 /
public class ThreadJoin {
public void buildThread() throws InterruptedException {
Adder a = new Adder("Test");
Thread t1 = new Thread(a, "First");
Thread t2 = new Thread(a, "second");
t1.start();
t2.start();
}
public static void main(String[] args) throws InterruptedException {
new ThreadJoin().buildThread();
}
}
class Adder implements Runnable {
String name;
int i;
private boolean suspendFlag = false;
public Adder(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
for(int i = 0; i < 10; i++){
put(i);
}
for(int j = 0 ; j < 10; j++){
get();
}
}
public synchronized void put(int i) {
this.i = i;
try {
while (suspendFlag) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("put: " + i+" currentThread: "+Thread.currentThread().getName());
suspendFlag = true;
notify();
}
public synchronized void get() {
System.out.println("Entering into the get method");
try {
if (!suspendFlag) {
wait();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("get: " + i+" currentThread: "+Thread.currentThread().getName());
suspendFlag = true;
notify();
}
}
哪個線程First
執行(例如First
)將執行synchronized
put
方法,從而鎖定Adder
對象的監視器。 在這種情況下,另一個線程( second
)無法開始執行put
方法,因為它無法鎖定同一Adder
對象的監視器。
因此, First
看到suspendFlag
為false
,因此跳過了while
。 它使supsedFlag
true
並調用notify()
。 當前沒有線程正在Adder
對象上等待,因此對notify()
的調用沒有任何作用。
當此線程結束put
方法的執行時,監視器將被釋放, second
可能獲取它。 如果發生這種情況,它將看到suspendFlag
為true
並進入while
塊,執行wait()
。 這樣做會釋放監視器。 suspendFlag
仍然為true
。
執行返回到First
, First
循環進行for
然后重新調用put
。 它看到suspendFlag
為true
並進入while
循環,並調用wait()
。
現在,您的兩個線程都處於wait
,沒有任何notify
。 您的程序停頓了。
發生的情況的簡單視圖是,您有一個等待通知結構,該結構強制進行交替的put
和get
調用。 您有兩個線程,每個線程put
在其第一個get
之前執行10次put
調用,因此無法進行交替。 這兩個線程最終都put
等待另一個進行get
。
你可以改變你的設計允許多put
電話沒有get
或更改驅動代碼,這樣一個線程並put
通話,另一種則get
調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.