老师下面代码循环调用notify哪里 Thread.sleep(100);注释掉阻塞顺序和唤醒顺序不一致,加上阻塞顺序和唤醒顺序一致。请问这是为什么
public class WaitNotifyWakeOrder implements Runnable{
private static Object lock = new Object();
private static List<String> waitList = new ArrayList<>();
private static List<String> notifyList = new ArrayList<>();
public static void main(String[] args) throws InterruptedException {
WaitNotifyWakeOrder wakeOrder = new WaitNotifyWakeOrder();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(wakeOrder);
thread.start();
}
Thread.sleep(100);
for (int i = 0; i < 10; i++) {
synchronized (lock){
lock.notify();
}
// Thread.sleep(100);
}
Thread.sleep(100);
System.out.println(waitList);
System.out.println(notifyList);
}
@Override
public void run() {
synchronized (lock){
System.out.println(Thread.currentThread().getName() + "准备阻塞");
try {
waitList.add(Thread.currentThread().getName());
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
notifyList.add(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName() + "被唤醒,执行完成");
}
}
}