问题 : 如果4个线程呢? A B C D 虚假唤醒
if 改为 while 就行了
while(number!=0){
//等待
this.wait();
}
number++;
System.out.println(Thread.currentThread().getName()+“==”+number);
//通知
this.notifyAll();
代码实现
package test01;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PC2 {
public static void main(String[] args) {
Data2 data2 = new Data2();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data2.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},“A”).start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data2.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},“B”).start();
new Thread(()->{
for (int i