import java.util.concurrent.TimeUnit;
class MyObject implements Runnable {
private Monitor monitor;
public MyObject(Monitor monitor) {
this.monitor = monitor;
}
public void run() {
try {
TimeUnit.SECONDS.sleep(3);
System.out.println("i'm going.");
monitor.gotMessage();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Monitor implements Runnable {
private volatile boolean go = false;
public synchronized void gotMessage() throws InterruptedException {
go = true;
notify();
}
public synchronized void watching() throws InterruptedException {
while (go == false) wait();
System.out.println("He has gone.");
}
public void run() {
try {
watching();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Wait {
public static void main(String[] args) {
Monitor monitor = new Monitor();
MyObject o = new MyObject(monitor);
new Thread(o).start();
new Thread(monitor).start();
}
}
关于java wait和notify的使用方式
最新推荐文章于 2024-05-30 10:13:24 发布