1.等待和唤醒是同一个对象
2.notify只会唤醒一个线程
使用角度
1.Object.wait() 会抛出 InterruptedException
2.调用object.wait() 必须首先对object(monitor lock) 进行加锁
class Main{
private static Object object = new Object();
private static class AThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("A 在扫地");
}
try {
synchronized (object) {
object.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println("A 正在擦桌子");
}
}
}