package threadobjectcommonsmethod;
public class WaitNotifyPrintOddEveWait {
static volatile int count = 0;
static final Object lock = new Object();
public static void main(String[] args) throws InterruptedException {
Runnable odd = () -> {
synchronized (lock){
try {
while(count <= 100 && count%2 == 0){
System.out.println(“偶数线程打印:”+count++);
lock.notify();
if(count <= 100){
lock.wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Runnable even = () -> {
synchronized (lock) {
try {
while(count <= 100 && count%2 == 1){
System.out.println(“奇数线程打印:”+count++);
lock.notify();
if(count <= 100){
lock.wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
new Thread(even).start();
new Thread(odd).start();
}
}
如果改变两个线程的启动顺序,就不会出现这个问题,搞不明白了,求老师解答