就是下面的代码是交替打印 0 到 100 的奇偶数,我这里的循环条件是 count 小于 100,为什么最后的输出结果会一直到 100 呢,不是 100 就跳出循环执行不到了吗,求大佬解答
public class WaitNotifyPrintOddEvenSyn {
private static int count;
private static final Object lock = new Object();
//新建 2 个线程,一个只处理偶数,一个只处理奇数
//并且用 synchronized 来通信
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
while(count < 100){
synchronized (lock){
if((count & 1) == 0){
System.out.println(Thread.currentThread().getName() + ": " + count);
count++;
}
}
}
}
}, "偶线程").start();
new Thread(new Runnable() {
@Override
public void run() {
while(count < 100){
synchronized (lock){
if((count & 1) == 1){
System.out.println(Thread.currentThread().getName() + ": " + count);
count++;
}
}
}
}
}, "奇线程").start();
}
}