[英]How to use wait() and notify() in Java?
據我所知,我想在互斥鎖上調用wait(),當我希望當前線程停止工作,直到另一個線程在同一個互斥對象上調用notify()。 這似乎不起作用。
我正在嘗試打印線程1-10。 然后等待另一個線程打印11-20。 然后第一個線程將再次打印21-30
Main.java
public class Main {
public static void main(String[] args) throws InterruptedException {
Object mutex = 1;
Thread child1 = new Thread(new Child1(mutex));
Thread child2 = new Thread(new Child2(mutex));
child1.start();
child2.start();
}
}
Child1.java
public class Child1 implements Runnable {
Object mutex;
public Child1(Object mutex){
this.mutex = mutex;
}
public void run() {
synchronized (mutex) {
for(int c = 0; c < 10; c++){
System.out.println(c+1);
}
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int c = 20; c < 31; c++){
System.out.println(c+1);
}
}
}
Child2.java
public class Child2 implements Runnable {
Object mutex;
public Child2(Object mutex) {
this.mutex = mutex;
}
public void run() {
synchronized (mutex) {
for (int c = 11; c < 21; c++) {
System.out.println(c);
}
notify();
}
}
}
產量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Exception in thread "Thread-0"
18
19
20
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at task42.Child1.run(Child1.java:18)
at java.lang.Thread.run(Thread.java:745)
java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at task42.Child2.run(Child2.java:15)
at java.lang.Thread.run(Thread.java:745)
我錯過了什么?
您必須將mutex
引用添加到wait()
和notify()
; 也就是說,將wait()
更改為mutex.wait()
並將notify()
更改為mutex.notify()
。
沒有這一點,您呼叫等待/通知對this
( method()
是等效於this.method()
以下是您的代碼,並進行了相應的更改:
Child1.java
public class Child1 implements Runnable {
Object mutex;
public Child1(Object mutex){
this.mutex = mutex;
}
public void run() {
synchronized (mutex) {
for(int c = 0; c < 10; c++){
System.out.println(c+1);
}
try {
mutex.wait(); // Changed here
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int c = 20; c < 31; c++){
System.out.println(c+1);
}
}
}
Child2.java
public class Child2 implements Runnable {
Object mutex;
public Child2(Object mutex) {
this.mutex = mutex;
}
public void run() {
synchronized (mutex) {
for (int c = 11; c < 21; c++) {
System.out.println(c);
}
mutex.notify(); // Changed here
}
}
}
您的代碼在幾個方面失敗。
首先,不能保證第一個線程也會先運行。 (特別是在多核上,很有可能兩者並行運行)。 因此,如果第二個線程首先進入Child2.run()
的synchronized
塊,它將在第一個線程處於等待狀態之前調用mutex.notify()
。 結果,第一個線程將永遠保留在mutex.wait()
。
其次, wait()
/ notify()
不被認為直接用作線程握手機制。 只有在第二個線程調用notify()
之前可以保證第一個線程調用wait()
,這才有效。 通常,你不能。
相反,應使用wait()
等待某個條件變為true。 該條件通常由另一個線程更改,該線程通過調用notifyAll()
通知等待線程。 所以握手機制是條件,而不是wait
/ notify
:
// 1st thread:
synchronized (lock) {
while (!condition) {
lock.wait();
}
// continue
}
// 2nd thread:
synchronized {
condition = true;
lock.notifyAll();
}
wait()
/ notify()
或notifyAll()
任何其他用法模式都是錯誤的! 始終在循環內調用wait()
也是非常重要的,因為線程可能會偶然喚醒 - 即使沒有notify()
或notifyAll()
。
使用wait()/notifyAll()
因此,在您的情況下,您可以將wait()
和notifyAll()
與stage-variable結合使用:
public class Mutex {
static final Object lock = new Object();
static int stage = 1;
static void first() throws InterruptedException {
synchronized (lock) {
// we're already in stage 1
for(int i = 0; i < 10; ++i) System.out.println(i);
// enter stage 2
stage = 2;
lock.notifyAll();
// wait for stage 3
while (stage != 3) { lock.wait(); }
// now we're in stage 3
for(int i = 20; i < 30; ++i) System.out.println(i);
}
}
static void second() throws InterruptedException {
synchronized (lock) {
// wait for stage 2
while (stage != 2) { lock.wait(); }
// now we're in stage 2
for(int i = 20; i < 30; ++i) System.out.println(i);
// enter stage 3
stage = 3;
lock.notifyAll();
}
}
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
try {
Mutex.first();
} catch (InterruptedException ex) { }
}
}).start();
new Thread(new Runnable() {
public void run() {
try {
Mutex.second();
} catch (InterruptedException ex) { }
}
}).start();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.