java收集Java学习笔记

Java 通过wait和notify实现生产者消费者模式

2016-12-23  本文已影响172人  专职跑龙套

wait 和 notify 的功能

线程进入 Waiting for monitor entry 状态后,一旦该对象被解锁,这些线程就会去竞争。

wait 和 notify 的使用

synchronized(obj) {
    while(some condition) {
      try {
        obj.wait();
      } catch(...) {...}
    }
}

通过wait和notify实现生产者消费者模式

代码如下:

public class ProducerCunsumer_Test {
    private static final int MAX_CAPACITY = 10;
    private static List<Object> goods = new ArrayList<Object>();

    public static void main(String[] args) {
        (new ProducerThread()).start();

        (new ConsumerThread()).start();
    }

    static class ProducerThread extends Thread {
        public void run() {
            while (true) {
                // 每隔 1000 毫秒生产一个商品
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }

                synchronized (goods) {
                    // 当前商品满了,生产者等待
                    if (goods.size() == MAX_CAPACITY) {
                        try {
                            System.out.println("Goods full, waiting...");
                            goods.wait();
                        } catch (Exception e) {
                        }
                    }

                    goods.add(new Object());
                    System.out.println("Produce goods, total: " + goods.size());

                    // goods.notify() 也可以
                    goods.notifyAll();
                }
            }
        }
    }

    static class ConsumerThread extends Thread {
        public void run() {
            while (true) {
                // 每隔 500 毫秒消费一个商品
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                }

                synchronized (goods) {
                    // 当前商品空了,消费者等待
                    if (goods.size() == 0) {
                        try {
                            System.out.println("No goods, waiting...");
                            goods.wait();
                        } catch (Exception e) {
                        }
                    }

                    goods.remove(0);
                    System.out.println("Consume goods, total: " + goods.size());

                    // goods.notify() 也可以
                    goods.notifyAll();
                }
            }
        }
    }
}

在上面的代码中,消费商品的速度(500毫秒)快于生产商品的速度(1000毫秒),依次输出如下所示:
可以看出,商品队列经常处于空的状态。

No goods, waiting...
Produce goods, total: 1
Consume goods, total: 0
No goods, waiting...
Produce goods, total: 1
Consume goods, total: 0
No goods, waiting...
Produce goods, total: 1
Consume goods, total: 0

如果修改,使得消费商品的速度(500毫秒)慢于生产商品的速度(100毫秒),依次输出如下所示:
可以看出,商品队列经常处于满的状态。

Produce goods, total: 1
Produce goods, total: 2
Produce goods, total: 3
Produce goods, total: 4
Produce goods, total: 5
Consume goods, total: 4
Produce goods, total: 5
Produce goods, total: 6
Produce goods, total: 7
Produce goods, total: 8
Consume goods, total: 7
Produce goods, total: 8
Produce goods, total: 9
Produce goods, total: 10
Goods full, waiting...
Consume goods, total: 9
Produce goods, total: 10
Goods full, waiting...
Consume goods, total: 9
Produce goods, total: 10
Goods full, waiting...
Consume goods, total: 9
Produce goods, total: 10
Goods full, waiting...

上一篇 下一篇

猜你喜欢

热点阅读