synchronized 版本,双线程,生产者消费者
public class 虚假唤醒 {
public static void main(String[] args) {
Data data=new Data();
new Thread(()->{
for (int i=0;i<10;i++)
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B").start();
new Thread(()->{
for (int i=0;i<10;i++)
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
}
}
class Data{
private int number=0;
public synchronized void increment() throws InterruptedException {
if (number!=0){
this.wait();
}
number++;
System.out.println(Thread.currentThread()+"