编程语言
首页 > 编程语言> > Java虚假唤醒及如何避免虚假唤醒

Java虚假唤醒及如何避免虚假唤醒

作者:互联网

先给出一段虚假唤醒的代码如下:

public class SynchronizeDemo2 {
 
    public static void main(String[] args) {
        Data2 data = new Data2();
 
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();
 
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();
 
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "D").start();
    }
 
}
 
//线程操作的资源类,判断等待业务、通知
class Data2 {
    private int number = 0;
 
    //+1
    public synchronized void increment() throws InterruptedException {
        if (number != 0) {//0
            //等待
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName() + "=>" + number);
        //通知其他线程,我+1完毕了
        this.notifyAll();
    }
 
    //-1
    public synchronized void decrement() throws InterruptedException {
        if (number == 0) {
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName() + "=>" + number);
        //通知其他线程,我-1完毕了
        this.notifyAll();
    }
 
 
}
 
//执行以上的代码结果输出如下:
A=>1
C=>0
B=>1
A=>2
B=>3
C=>2
C=>1
C=>0
B=>1
A=>2
B=>3
C=>2
B=>3
A=>4
D=>3
D=>2
D=>1
D=>0
A=>1
D=>0

什么叫虚假唤醒?

          站在上述两个消费者线程的角度上讲, 无论哪一个线程抢到了资源, 另一个线程的唤醒就可以被认为是没有必要的, 也就是被虚假唤醒了。

虚假唤醒发生的场景以及解决方式?
         上述代码使用if判断,唤醒后线程会从wait之后的代码开始运行,但是不会重新判断if条件,直接继续运行if代码块之后的代码,而如果使用while的话,也会从wait之后的代码运行,但是唤醒后会重新判断循环条件,如果不成立再执行while代码块之后的代码块,成立的话继续wait。
 

解决虚假唤醒生产者消费者实例代码:

public class SynchronizeDemo3 {
 
    public static void main(String[] args) {
        Data3 data = new Data3();
 
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();
 
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();
 
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();
 
        new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "D").start();
    }
 
}
 
//线程操作的资源类,判断等待业务、通知
class Data3 {
    private int number = 0;
 
    //+1
    public synchronized void increment() throws InterruptedException {
        while (number != 0) {//0
            //等待
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName() + "=>" + number);
        //通知其他线程,我+1完毕了
        this.notifyAll();
    }
 
    //-1
    public synchronized void decrement() throws InterruptedException {
        while (number == 0) {
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName() + "=>" + number);
        //通知其他线程,我-1完毕了
        this.notifyAll();
    }
 
 
}
//线程操作的资源类,判断等待业务、通知
class Data3 {
    private int number = 0;
 
    //+1
    public synchronized void increment() throws InterruptedException {
        while (number != 0) {//0
            //等待
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName() + "=>" + number);
        //通知其他线程,我+1完毕了
        this.notifyAll();
    }
 
    //-1
    public synchronized void decrement() throws InterruptedException {
        while (number == 0) {
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName() + "=>" + number);
        //通知其他线程,我-1完毕了
        this.notifyAll();
    }
 
}
 
//多次运行代码输出如下:
C=>1
B=>0
C=>1
A=>0
D=>1
B=>0
C=>1
A=>0
D=>1
B=>0
C=>1
A=>0
D=>1
B=>0
C=>1
B=>0
D=>1
A=>0
D=>1
A=>0

标签:Java,Thread,++,虚假,number,InterruptedException,int,线程,唤醒
来源: https://blog.csdn.net/xjk201/article/details/121608931