睡眠/唤醒一个线程coding
Object obj = new Object();
Thread t1 = new Thread(() -> {
System.out.println("thread wait === 1");
try {
synchronized (obj) {
obj.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread await === 1");
});
Thread t2 = new Thread(() -> {
System.out.println("thread wait === 2");
try {
synchronized (obj) {
obj.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread await === 2");
});
t1.start();
t2.start();
Thread.sleep(10);
synchronized (obj) {
obj.notifyAll();
}