wait,notify/notifyAll都要放在synchronized锁里面
如果没放在synchronized锁里面,就要报这样的错误
public class Test5 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(()->{
syn();
},"t1");
Thread t2 = new Thread(()->{
syn();
},"t2");
t1.start();
t2.start();
Thread.sleep(12000);
synchronized (Test5.class){
Test5.class.notifyAll();
}
}
public static synchronized void syn(){
try {
for (int i = 0; i < 10; i++) {
if(i == 5){
Test5.class.wait();
}
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}