• notify和notifyall的区别?


    两个概念:

    1、锁池EntryList

    2、等待池 WaitSet

    注意:锁池和等待池都是针对对象的

    问题:notify和notifyall的区别?

    -----》1、notifyAll会让所有处于等待池的线程全部进入锁池去竞争获取锁的机会

    2、notify只会随机选取一个处于等待池的线程进入锁池去竞争获取锁的机会

    public class NotificationDemo {
        private volatile boolean go = false;
    
        public static void main(String args[]) throws InterruptedException {
            final NotificationDemo test = new NotificationDemo();
    
            Runnable waitTask = new Runnable(){
    
                @Override
                public void run(){
                    try {
                        test.shouldGo();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " finished Execution");
                }
            };
    
            Runnable notifyTask = new Runnable(){
    
                @Override
                public void run(){
                    test.go();
                    System.out.println(Thread.currentThread().getName() + " finished Execution");
                }
            };
    
            Thread t1 = new Thread(waitTask, "WT1"); //will wait
            Thread t2 = new Thread(waitTask, "WT2"); //will wait
            Thread t3 = new Thread(waitTask, "WT3"); //will wait
            Thread t4 = new Thread(notifyTask,"NT1"); //will notify
    
            //starting all waiting thread
            t1.start();
            t2.start();
            t3.start();
    
            //pause to ensure all waiting thread started successfully
            Thread.sleep(200);
    
            //starting notifying thread
            t4.start();
    
        }
        /*
         * wait and notify can only be called from synchronized method or bock
         */
        private synchronized void shouldGo() throws InterruptedException {
            while(go != true){
                System.out.println(Thread.currentThread()
                        + " is going to wait on this object");
                wait(); //release lock and reacquires on wakeup
                System.out.println(Thread.currentThread() + " is woken up");
            }
            go = false; //resetting condition
        }
    
        /*
         * both shouldGo() and go() are locked on current object referenced by "this" keyword
         */
        private synchronized void go() {
            while (go == false){
                System.out.println(Thread.currentThread()
                        + " is going to notify all or one thread waiting on this object");
    
                go = true; //making condition true for waiting thread
                //notify(); // only one out of three waiting thread WT1, WT2,WT3 will woke up
                notifyAll(); // all waiting thread  WT1, WT2,WT3 will woke up
            }
    
        }
    }

    ----》屏蔽notifyAll()方法,打开notify()方法,结果如下:

    Thread[WT1,5,main] is going to wait on this object
    Thread[WT2,5,main] is going to wait on this object
    Thread[WT3,5,main] is going to wait on this object
    Thread[NT1,5,main] is going to notify all or one thread waiting on this object
    NT1 finished Execution
    Thread[WT1,5,main] is woken up
    WT1 finished Execution

    -------》屏蔽notify方法,打开notifyAll()方法,结果如下:

    Thread[WT1,5,main] is going to wait on this object
    Thread[WT2,5,main] is going to wait on this object
    Thread[WT3,5,main] is going to wait on this object
    Thread[NT1,5,main] is going to notify all or one thread waiting on this object
    Thread[WT3,5,main] is woken up
    WT3 finished Execution
    Thread[WT2,5,main] is woken up
    Thread[WT2,5,main] is going to wait on this object
    Thread[WT1,5,main] is woken up
    Thread[WT1,5,main] is going to wait on this object
    NT1 finished Execution

      

  • 相关阅读:
    Sublime Text 3
    JobTracker等相关功能模块初始化
    .NET编程规范
    理解多线程设计模式(转)
    理解java中的ThreadLocal 专题
    情商--人生职场
    老师只喜欢好学生(转)
    不是因为项目让你不能发光,而是因为你才让项目不能发光
    考试系统--前进/后退功能
    tomcat配置文件server.xml具体解释
  • 原文地址:https://www.cnblogs.com/vingLiu/p/10664343.html
Copyright © 2020-2023  润新知