notify和notifyall的区别

讲解之前先贴上一篇代码,如下代码线程A中执行lock.wait()方法,不设置等待时间,设置无限等待

public static void main(String[] args) {
        final Object lock = new Object();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread A is waiting to get lockk");
                synchronized (lock) {
                    try {
                        System.out.println("thread A get lock");
                        Thread.sleep(20);
                        System.out.println("thread A do wait method");
                        lock.wait();
                        System.out.println("thread A is done");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread B is waiting to get lockk");
                synchronized (lock) {
                    try {
                        System.out.println("thread B get lock");
                        System.out.println("thread B is sleeping 10ms");
                        Thread.sleep(10);
                        System.out.println("thread B is done");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

执行结果:执行了之后发现程序永远停不下来,因为设置了无限等待
在这里插入图片描述

使用notify唤醒程序

public static void main(String[] args) {
        final Object lock = new Object();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread A is waiting to get lockk");
                synchronized (lock) {
                    try {
                        System.out.println("thread A get lock");
                        Thread.sleep(20);
                        System.out.println("thread A do wait method");
                        lock.wait();
                        System.out.println("thread A is done");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread B is waiting to get lockk");
                synchronized (lock) {
                    try {
                        System.out.println("thread B get lock");
                        System.out.println("thread B is sleeping 10ms");
                        Thread.sleep(10);
                        System.out.println("thread B is done");
                        lock.notify();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

在这里插入图片描述
执行过程:

  1. System.out.println(“thread A is waiting to get lockk”);
  2. 执行System.out.println(“thread A get lock”);
  3. 执行Thread.sleep(20);
  4. 由于20ms大于10ms,因此线程B开始执行
  5. 执行 System.out.println(“thread B is waiting to get lockk”);
  6. 由于线程B要获取同步锁,此时我们的同步锁已经被A获取,因此线程B只能去等待,A继续执行
  7. 执行System.out.println(“thread A do wait method”);
  8. 线程A执行wait的时候,线程B开始执行,由于wait会释放锁,线程B获取到线程锁,线程B开始执行,而线程A进行无限等待
  9. 执行System.out.println(“thread B get lock”);
  10. 执行System.out.println(“thread B is sleeping 10ms”);
  11. 执行Thread.sleep(20);
  12. 执行System.out.println(“thread B is done”);
  13. 唤醒线程A
  14. 执行System.out.println(“thread A is done”);

notifyAll()也能达到同样的效果

notify和notifyAll的区别

首先讲这俩区别之前先来了解两个概念。锁池EntryList和等待池WaitSet。而这两个池和Object基类的notify

锁池

假设线程A已经拥有了某个对象(不是类)的锁,而其它线程B、C想要调用这个对象的某个synchronized方法(或者代码块), 由于B、C线程在进入对象的synchronized方法(或者块)之前必须先获得该对象锁的拥有权,而恰巧该对象的锁目前正被线程A所占用,此时B、C线程就会被阻塞,进入一个地方去等待锁的释放,这个地方便是该对象的锁池

等待池

假设线程A调用了某个对象的wait()方法,线程A就会释放该对象的锁,同时线程A就进入到了该对象的等待池中,进入到等待池中的线程不会去竞争该对象的锁。

notifyAll会让所有处于等待池的线程全部进入锁池去竞争获取锁的机会
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();

        Thread.sleep(200);

        t4.start();
    }

    private synchronized void shouldGo() throws InterruptedException {
        while (go != true) {
            System.out.println(Thread.currentThread() + " is going to wait on this object ");
            wait();
            System.out.println(Thread.currentThread() + " is woken up");
        }
        go = false;
    }

    private synchronized void go() {
        while (go == false) {
            System.out.println(Thread.currentThread() + "is going to notify all or one thread wating on thread");
            go = true;
            notify();
//            notifyAll();
        }
    }
}

当我们使用notify唤醒线程,执行结果:
在这里插入图片描述
线程WT2和线程WT3未被唤醒

当我们使用notifyAll唤醒线程, 执行结果:
在这里插入图片描述
WT1, WT2, WT3都被唤醒,将所有等待池的线程都放入锁池,去争夺获取锁的机会去执行任务

  • 8
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值