当前位置:网站首页>JUC learning record (2022.4.22)

JUC learning record (2022.4.22)

2022-04-23 15:00:00 Binbin ice

1、 spurious wakeup

Multi-threaded environment , There are multiple threads executing wait() Method , Other threads are required to execute notify() perhaps notifyAll() Ways to wake them up , If multiple threads are awakened , But only some of them are useful wake-up operations , The rest of the awakening is useless ; For threads that should not be awakened , Is false awakening .

package sync;
/**  When wait stay if Intra statement time , Because the conditions have been judged before entering , So when you wake up ( Where to sleep , Where do you wake up ) You won't judge , Continue with the following statement , Both send false wake-up  */
class Calculator {
    
    private int num = 0;

    public synchronized void increment() throws InterruptedException {
    
        //if (num != 0) { // There is a false awakening 
        // this.wait();
        //}
        while (num != 0) {
      // Every time I wake up, I judge again , Solve false awakening 
            this.wait();
        }
        num++;
        System.out.println(Thread.currentThread().getName() + "+1, Current value :" + num);
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException {
    
        //if (num != 1) { // There is a false awakening 
        // this.wait();
        //}
        while (num != 1) {
      // Every time I wake up, I judge again , Solve false awakening 
            this.wait();
        }
        num--;
        System.out.println(Thread.currentThread().getName() + "-1, Current value :" + num);
        this.notifyAll();
    }

}

public class Compute {
    
    public static void main(String[] args) {
    
        Calculator calculator = new Calculator();
        new Thread(() -> {
    
            try {
    
                for (int i = 0; i < 10; i++) {
    
                    calculator.increment();
                }
            } catch (InterruptedException e) {
    
                e.printStackTrace();
            }
        }, "AA").start();
        new Thread(() -> {
    
            try {
    
                for (int i = 0; i < 10; i++) {
    
                    calculator.decrement();
                }
            } catch (InterruptedException e) {
    
                e.printStackTrace();
            }
        }, "BB").start();
        new Thread(() -> {
    
            try {
    
                for (int i = 0; i < 10; i++) {
    
                    calculator.increment();
                }
            } catch (InterruptedException e) {
    
                e.printStackTrace();
            }
        }, "CC").start();
        new Thread(() -> {
    
            try {
    
                for (int i = 0; i < 10; i++) {
    
                    calculator.decrement();
                }
            } catch (InterruptedException e) {
    
                e.printStackTrace();
            }
        }, "DD").start();
    }
}

版权声明
本文为[Binbin ice]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231457175055.html