博客
关于我
求求你,别再用wait和notify了!
阅读量:459 次
发布时间:2019-03-06

本文共 5412 字,大约阅读时间需要 18 分钟。

为什么不能用 wait 和 notify 了?

在JDK 1.5中,Condition类被引入来替代wait和notify方法,这让很多开发者感到困惑。毕竟wait和notify是经典的线程通讯工具,似乎已经非常完美了。那么,为什么会推荐使用Condition而不是传统的wait和notify呢?让我来为你详细解答这个问题。

Condition 的优势

  • 避免线程“假死”

    notify方法在极端环境下可能会导致线程“假死”。这种情况通常发生在多个生产者和一个消费者的场景下。当一个生产者被唤醒后,它会通知所有等待的线程,包括其他生产者和消费者。假设消费者的任务尚未完成,其他生产者却被唤醒并进入等待状态,这样就会导致生产者的线程被意外地唤醒,进而引发程序的阻塞。

  • 性能更高

    Condition方法的性能比wait和notify更优。这是因为Condition实现了更高效的等待和唤醒机制,减少了不必要的线程操作,提高了程序的执行效率。

  • notify 线程“假死”的问题

    让我们通过经典的生产者和消费者模型来理解这一问题。

    正常版本

    在下面的代码中,使用wait和notify实现了生产者和消费者的交替工作:

    class Factory {    private int[] items = new int[1];    private int size = 0;    public synchronized void put() throws InterruptedException {        do {            while (size == items.length) {                System.out.println(Thread.currentThread().getName() + " 进入阻塞");                this.wait();                System.out.println(Thread.currentThread().getName() + " 被唤醒");            }            System.out.println(Thread.currentThread().getName() + " 开始工作");            items[0] = 1;            size++;            System.out.println(Thread.currentThread().getName() + " 完成工作");            this.notify();        } while (true);    }    public synchronized void take() throws InterruptedException {        do {            while (size == 0) {                System.out.println(Thread.currentThread().getName() + " 进入阻塞(消费者)");                this.wait();                System.out.println(Thread.currentThread().getName() + " 被唤醒(消费者)");            }            System.out.println("消费者工作~");            size--;            this.notify();        } while (true);    }}public class NotifyDemo {    public static void main(String[] args) {        Factory factory = new Factory();        Thread producer = new Thread(() -> {            try {                factory.put();            } catch (InterruptedException e) {                e.printStackTrace();            }        }, "生产者");        producer.start();        Thread consumer = new Thread(() -> {            try {                factory.take();            } catch (InterruptedException e) {                e.printStackTrace();            }        }, "消费者");        consumer.start();    }}

    线程假死版本

    当我们增加两个生产者时,就会遇到线程假死的问题:

    public class NotifyDemo {    public static void main(String[] args) {        Factory factory = new Factory();        Thread producer = new Thread(() -> {            try {                factory.put();            } catch (InterruptedException e) {                e.printStackTrace();            }        }, "生产者");        producer.start();        Thread producer2 = new Thread(() -> {            try {                factory.put();            } catch (InterruptedException e) {                e.printStackTrace();            }        }, "生产者2");        producer2.start();        Thread consumer = new Thread(() -> {            try {                factory.take();            } catch (InterruptedException e) {                e.printStackTrace();            }        }, "消费者");        consumer.start();    }}

    在这个版本中,生产者2在没有数据时被唤醒,导致线程假死,整个程序无法继续执行。

    使用 Condition

    为了避免线程假死问题,我们可以使用Condition类。Condition类提供了更灵活的等待和唤醒机制,可以避免不必要的线程唤醒。

    使用 Condition 的代码示例

    class FactoryByCondition {    private int[] items = new int[1];    private int size = 0;    private Lock lock = new ReentrantLock();    private Condition producerCondition = lock.newCondition();    private Condition consumerCondition = lock.newCondition();    public void put() throws InterruptedException {        do {            lock.lock();            while (size == items.length) {                System.out.println(Thread.currentThread().getName() + " 进入阻塞");                producerCondition.await();                System.out.println(Thread.currentThread().getName() + " 被唤醒");            }            System.out.println(Thread.currentThread().getName() + " 开始工作");            items[0] = 1;            size++;            System.out.println(Thread.currentThread().getName() + " 完成工作");            consumerCondition.signal();        } while (true);    }    public void take() throws InterruptedException {        do {            lock.lock();            while (size == 0) {                System.out.println(Thread.currentThread().getName() + " 进入阻塞(消费者)");                consumerCondition.await();            }            System.out.println("消费者工作~");            size--;            producerCondition.signal();        } while (true);    }}

    多线程测试

    public class NotifyDemo {    public static void main(String[] args) {        FactoryByCondition factory = new FactoryByCondition();        Thread producer = new Thread(() -> {            try {                factory.put();            } catch (InterruptedException e) {                e.printStackTrace();            }        }, "生产者");        producer.start();        Thread producer2 = new Thread(() -> {            try {                factory.put();            } catch (InterruptedException e) {                e.printStackTrace();            }        }, "生产者2");        producer2.start();        Thread consumer = new Thread(() -> {            try {                factory.take();            } catch (InterruptedException e) {                e.printStackTrace();            }        }, "消费者");        consumer.start();    }}

    性能对比

    如果我们使用 notifyAll 替代 notify,虽然可以避免线程假死,但会带来性能问题。因为 notifyAll 会唤醒所有等待的线程,包括无需执行任务的生产者,导致资源浪费。

    总结

    Condition类提供了一种更安全和高效的线程通讯方式,避免了wait和notify方法的潜在问题。如果你正在进行并发编程,尤其是在涉及多个生产者和消费者的场景中,使用Condition类是更好的选择。

    转载地址:http://ewwbz.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现CircularQueue循环队列算法(附完整源码)
    查看>>
    Objective-C实现clearBit清除位算法(附完整源码)
    查看>>
    Objective-C实现climbStairs爬楼梯问题算法(附完整源码)
    查看>>
    Objective-C实现cocktail shaker sort鸡尾酒排序算法(附完整源码)
    查看>>
    Objective-C实现cocktailShakerSort鸡尾酒排序算法(附完整源码)
    查看>>
    Objective-C实现CoinChange硬币兑换问题算法(附完整源码)
    查看>>
    Objective-C实现collatz sequence考拉兹序列算法(附完整源码)
    查看>>
    Objective-C实现Collatz 序列算法(附完整源码)
    查看>>
    Objective-C实现comb sort梳状排序算法(附完整源码)
    查看>>
    Objective-C实现combinationSum组合和算法(附完整源码)
    查看>>
    Objective-C实现combinations排列组合算法(附完整源码)
    查看>>
    Objective-C实现combine With Repetitions结合重复算法(附完整源码)
    查看>>
    Objective-C实现combine Without Repetitions不重复地结合算法(附完整源码)
    查看>>
    Objective-C实现conjugate gradient共轭梯度算法(附完整源码)
    查看>>
    Objective-C实现connected components连通分量算法(附完整源码)
    查看>>
    Objective-C实现Connected Components连通分量算法(附完整源码)
    查看>>
    Objective-C实现Convex hull凸包问题算法(附完整源码)
    查看>>
    Objective-C实现convolution neural network卷积神经网络算法(附完整源码)
    查看>>
    Objective-C实现convolve卷积算法(附完整源码)
    查看>>
    Objective-C实现coulombs law库仑定律算法(附完整源码)
    查看>>