并发:阻塞队列与生产者消费者模型

  对于生产者消费者模型,如果我们采用常规的容器,那么我们需要同步,并且需要在生产者线程和消费者线程之间做wait和notify的线程间通信。而阻塞队列将同步操作和生产者和消费者间的通信在容器内部实现,使用起来就不需要我们自己做同步和线程通信了。
  Java中提供的BlockingQueue包括:
  ArrayBlockingQueue、LinkedBlockingQueue、PriorityBlockingQueue。
  一个典型的应用场景就是socket客户端数据的读取和解析。

另外关于生产者-消费者模型,下面是采用Object.wait(),Object.notify()非阻塞容器实现的生产者-消费者模式:
摘抄自此

public class Test {
    private int queueSize = 10;
    private PriorityQueue<Integer> queue = new PriorityQueue<Integer>(queueSize);

    public static void main(String[] args)  {
        Test test = new Test();
        Producer producer = test.new Producer();
        Consumer consumer = test.new Consumer();

        producer.start();
        consumer.start();
    }

    class Consumer extends Thread{

        @Override
        public void run() {
            consume();
        }

        private void consume() {
            while(true){
                synchronized (queue) {
                    while(queue.size() == 0){
                        try {
                            System.out.println("队列空,等待数据");
                            queue.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            queue.notify();
                        }
                    }
                    queue.poll();          //每次移走队首元素
                    queue.notify();
                    System.out.println("从队列取走一个元素,队列剩余"+queue.size()+"个元素");
                }
            }
        }
    }

    class Producer extends Thread{

        @Override
        public void run() {
            produce();
        }

        private void produce() {
            while(true){
                synchronized (queue) {
                    while(queue.size() == queueSize){
                        try {
                            System.out.println("队列满,等待有空余空间");
                            queue.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            queue.notify();
                        }
                    }
                    queue.offer(1);        //每次插入一个元素
                    queue.notify();
                    System.out.println("向队列取中插入一个元素,队列剩余空间:"+(queueSize-queue.size()));
                }
            }
        }
    }
}
发布了59 篇原创文章 · 获赞 9 · 访问量 7万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章