public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
final WaitAndNotify waitAndNotify = new WaitAndNotify();
new Thread() {
public void run() {
synchronized (waitAndNotify) {
try {
waitAndNotify.callWait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
synchronized (waitAndNotify) {
waitAndNotify.callNotify();
}
}
}.start();
}
}
class WaitAndNotify {
public void callWait() throws InterruptedException {
System.out.println("等待开始");
this.wait();
System.out.println("等待结束");
}
public void callNotify() {
System.out.println(" 通知开始");
this.notify();
System.out.println(" 通知结束");
}
}
通过这个例子一眼就看懂java中wait和notify()用法
最新推荐文章于 2022-06-28 00:57:48 发布