java面试 多线程:notify为什么会导致死锁。而notifyAll不会?

前提:notify导致的死锁不是争夺资源引起的死锁

首先我们要知道。在Java中当线程使用了wati 、 notify后,锁就自动升级为总量级锁了,这时会使用操作系统提供的monitor对象。monitor对象内有waitSet和entryList.
其中 waitSet 中的对象,只能由notify或者notifyAll唤醒后才会去争夺锁。
而entryList中的对象,则是有机会就会去尝试获取锁。


notify每次只随机唤醒waitSet内的一个线程
notifyAll 会将waitSet内的所有线程都唤醒


class OutTurn {
    private boolean isSub = true;
    private int count = 0;
    public synchronized void sub() {
        try {
            while (!isSub) {
                this.notify();
                System.out.println("b");
                this.wait();
            }
            System.out.println("sub ---- " + count);
            isSub = false;
            this.notify();
        } catch (Exception e) {
            e.printStackTrace();
        }
        count++;
    }

    public synchronized void main() {
        try {
            while (isSub) {
                this.notify();
                System.out.println("a");
                this.wait();
            }
            System.out.println("main ( " + count);
            isSub = true;
            this.notify();
        } catch (Exception e) {
            e.printStackTrace();
        }
        count++;
    }
}

class LockDemo {
    public static void main(String[] args) {

        final OutTurn ot = new OutTurn();

        for (int j = 0; j < 100; j++) {

            new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 5; i++) {
                        ot.sub();
                    }
                }
            }).start();
            new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 5; i++) {
                        ot.main();
                    }
                }
            }).start();
        }
    }
}

在上面代码的场景中,我们假设sub为生产者main为消费者

当waitSet中有大量的 sub 和 main 时, 此时如果有一个生产者获取了锁对象 之后发现不需要生产了,之后释放锁然后notify此时就很可能会再次唤醒生产者。这就是notify导致死锁的原因了。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值