- synchronized 版本,双线程,生产者消费者
- 运行结果
- synchronized 版本,四线程, 生产者消费者
- 运行结果
- 这里,居然,卡住了,不知道为啥
- synchronized 版本,四线程, 生产者消费者,使用 while 判断
- 运行结果
- 总结
- 参考目录
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;
//+1
public synchronized void increment() throws InterruptedException {
if (number!=0){ //0
//等待
this.wait();
}
number++;
System.out.println(Thread.currentThread()+"=>"+number);
//通知其他线程,我+1完毕了
this.notifyAll();
}
//-1
public synchronized void decrement() throws InterruptedException {
if (number==0){ //1
//等待
this.wait();
}
number--;
System.out.println(Thread.currentThread()+"=>"+number);
//通知其他线程,我-1完毕了
this.notifyAll();
}
}
运行结果
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();
new Thread(()->{
for (int i=0;i<10;i++)
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"C").start();
new Thread(()->{
for (int i=0;i<10;i++)
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"D").start();
}
}
//判断等待,业务,通知
class Data{ //数字 资源类
private int number=0;
//+1
public synchronized void increment() throws InterruptedException {
if (number!=0){ //0
//等待
this.wait();
}
number++;
System.out.println(Thread.currentThread()+"=>"+number);
//通知其他线程,我+1完毕了
this.notifyAll();
}
//-1
public synchronized void decrement() throws InterruptedException {
if (number==0){ //1
//等待
this.wait();
}
number--;
System.out.println(Thread.currentThread()+"=>"+number);
//通知其他线程,我-1完毕了
this.notifyAll();
}
}
运行结果
这里,居然,卡住了,不知道为啥
synchronized 版本,四线程, 生产者消费者,使用 while 判断
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();
new Thread(()->{
for (int i=0;i<10;i++)
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"C").start();
new Thread(()->{
for (int i=0;i<10;i++)
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"D").start();
}
}
//判断等待,业务,通知
class Data{ //数字 资源类
private int number=0;
//+1
public synchronized void increment() throws InterruptedException {
while (number!=0){ //0
//等待
this.wait();
}
number++;
System.out.println(Thread.currentThread()+"=>"+number);
//通知其他线程,我+1完毕了
this.notifyAll();
}
//-1
public synchronized void decrement() throws InterruptedException {
while (number==0){ //1
//等待
this.wait();
}
number--;
System.out.println(Thread.currentThread()+"=>"+number);
//通知其他线程,我-1完毕了
this.notifyAll();
}
}
运行结果
总结
可以看出,在需要判断来进行 Wait() 的情况下,不要使用 if 的一次判断,最好使用 while 的循环判断。
因为,当一个 线程,因为 if 判断符合,而进入 if 来 Wait() 后,只要有 线程 notifyAll() 来唤醒这个线程时,就不会再次进行 if 判断,从而 逃脱 条件判断。因为 wait notify后,会从 wait 的地方 继续执行。而 while 可以 防止这种情况的发生。
参考目录狂神
https://www.bilibili.com/video/BV1B7411L7tE?p=7&spm_id_from=pageDriver
说的比较详细的 博客
https://blog.csdn.net/qq_39455116/article/details/87101633



