最近正在學多執行緒現在卡在一個地方是 A執行緒跑完以後B執行緒不會被喚醒以下是程式碼:
class Athread implements Runnable {
public void run() {
synchronized (this) {
try {
System.out.println("A點進入");
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
this.notifyAll();
} catch (Exception e) {
}
}
}
}
class Bthread implements Runnable {
public void run() {
synchronized (this) {
try {
this.wait();
System.out.println("B點進入");
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
} catch (Exception e) {
}
}
}
}
public static void main(String[] args) {
Athread a = new Athread();
Bthread b = new Bthread();
Thread thr = new Thread(a);
Thread thr1 = new Thread(b);
thr.start();
thr1.start();
}
想請問各位我是否有地方理解錯了呢?
因為就我的理解是跑到notifyAll以後所有在wait()的執行緒都要被喚醒這樣
想請各位糾正一下
謝謝各位