• java:为什么要总是使用对象的notifyAll而不使用notify?


    最佳实践:总是使用notifyAll而不使用notify
    示例代码:交替打印0和1的程序

    package com.example.app;
    
    import java.util.Date;
    
    public class MyCounter2 {
        private int counter;
    
        public int getCounter() {
            return counter;
        }
    
        public void setCounter(int counter) {
            this.counter = counter;
        }
    
        public synchronized void increase(){
            String name=Thread.currentThread().getName();
            System.out.println(System.currentTimeMillis()+" in thread: "+name+ " "+this.getCounter());
            while(this.getCounter()>0){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.counter++;
            System.out.println(System.currentTimeMillis()+" "+name+" "+this.counter);
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.notify();
        }
    
        public synchronized void decrease(){
            String name=Thread.currentThread().getName();
            System.out.println(System.currentTimeMillis()+" in thread: "+name+ " "+this.getCounter());
            while(this.getCounter()<1){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.counter--;
            System.out.println(System.currentTimeMillis()+" "+name+" "+this.counter);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.notify();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    package com.example.app;
    
    public class NotifyTest2 {
        private static MyCounter2 myCounter = new MyCounter2();
        public static void main(String[] args) throws InterruptedException {
            Thread t1 = new ThreadA(myCounter);
    
           Thread t2= new ThreadB(myCounter);
    
           Thread t3=new ThreadC(myCounter);
           Thread t4 = new ThreadD(myCounter);
    
            t1.start();
            t2.start();
            t3.start();
            t4.start();
    
            t1.join();
            t2.join();
            t3.join();
            t4.join();
    
        }
    }
    
    class ThreadA extends Thread{
        private MyCounter2 myCounter;
        public ThreadA(MyCounter2 myCounter){
            this.myCounter=myCounter;
            setName("thread-A");
        }
        @Override
       public void run(){
            while(true){
                myCounter.increase();
            }
        }
    }
    
    class ThreadB extends Thread{
        private MyCounter2 myCounter;
        public ThreadB(MyCounter2 myCounter){
            this.myCounter=myCounter;
            setName("thread-B");
        }
        @Override
        public void run(){
            while(true){
                myCounter.decrease();
            }
        }
    }
    
    
    
    class ThreadC extends Thread{
        private MyCounter2 myCounter;
        public ThreadC(MyCounter2 myCounter){
            this.myCounter=myCounter;
            setName("thread-C");
        }
        @Override
        public void run(){
            while(true){
                myCounter.increase();
            }
        }
    }
    
    class ThreadD extends Thread{
        private MyCounter2 myCounter;
        public ThreadD(MyCounter2 myCounter){
            this.myCounter=myCounter;
            setName("thread-D");
        }
        @Override
        public void run(){
            while(true){
                myCounter.decrease();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    一次执行结果及执行时序图分析
    在这里插入图片描述
    可见,使用notify时,可能最后4个线程都处于锁对象的等待队列(WaiSet)中,程序阻塞。原因是notify只会唤醒wait set中的1个线程。
    将程序中的notify改成notifyAll,就不会出现上述的4个线程都处于wait set中的情况。

  • 相关阅读:
    基于SSM+Vue的高校实验室管理系统的设计与实现
    罗马和汉朝的对比
    5G 时代来了;还不赶紧学习音视频?
    【Highway-env】IntersectionEnv代码阅读
    分布式系统(二)分布式事务的理解
    WebSocket开发(客服对话)功能
    高防虚拟主机怎么选?
    PE文件硬编码代码注入
    Cadence23学习笔记(三)
    【从零开始学习 SystemVerilog】2.12、SystemVerilog 数据类型—— Queue(队列)
  • 原文地址:https://blog.csdn.net/amadeus_liu2/article/details/126259555