java 阻塞隊列

一:常見阻塞隊列
ArrayBlockingQueue 實現基於數組 ,使用時 必須指明大小
LinkedBlockingQueue 實現基於鏈表 ,默認大小爲 Integer.MAX_VALUE
PriorityBlockingQueue 實現基於優先隊列 可以無限大
DelayQueue 基於 優先隊列,一種延時隊列,只有當指定的時間到了,纔可以從隊列中獲取元素

二:常用的方法

  1. 非阻塞隊列
    add 添加失敗 會拋出異常
    remove 刪除失敗,跑出異常
    offer 添加失敗,返回 false
    poll 刪除失敗,返回 null
    peek
    建議使用 offer 和poll

  2. 阻塞隊列
    take 如果空,則等待
    put 如果 滿 ,則等待
    offer(E e,time) 超出 time 則執行失敗 返回 false
    pull (time) null

三:使用 Object.wait Object.notify 實現 生產者消費者模式

package concurrent;

import java.util.PriorityQueue;
import java.util.concurrent.ArrayBlockingQueue;

/*
* @author: wjf
* @version: 2016年3月30日 下午12:45:16
*/

public class TestArrayBlockingQueue {
    private static PriorityQueue<Integer> queue=new PriorityQueue<Integer>();
    private static int queuesize=10;
    public static void main(String[] args){
//      ArrayBlockingQueue<Integer> blockqueue=new ArrayBlockingQueue<Integer>(1);
        Producer p=new Producer(TestArrayBlockingQueue.queue,TestArrayBlockingQueue.queuesize);
        Consumer c=new Consumer(TestArrayBlockingQueue.queue,TestArrayBlockingQueue.queuesize);
        p.start();
        c.start();
    }

}
class Producer extends Thread{
    private PriorityQueue<Integer> queue;
    private int size;
    public Producer(PriorityQueue<Integer> queue,int size){
        this.queue=queue;
        this.size=size;
    }
    public void run(){
        produce();
    }
    private void produce(){
        while(true){
            synchronized(queue){
                if(queue.size()==size){
                    System.out.println("the queue is full,pleast wait");
                    try{
                        queue.wait();
                    }catch(InterruptedException e){
                        e.printStackTrace();
                        queue.notify();
                    }

                }
                else{
                    queue.offer(new Integer(1));
                    queue.notify();
                    System.out.println("produce a new item,the left size is: "+(size-queue.size()));
                }
            }
        }
    }
}
class Consumer extends Thread{
    private PriorityQueue<Integer> queue;
    private int size;
    public Consumer(PriorityQueue<Integer> queue,int size){
        this.queue=queue;
        this.size=size;
    }
    public void run(){
        consum();
    }
    private void consum(){
        while(true){
            synchronized(queue){
                if(queue.size()==0){
                    System.out.println("the queue is empty,pleast wait");
                    try{
                        queue.wait();
                    }catch(InterruptedException e){
                        e.printStackTrace();
                        queue.notify();
                    }

                }
                else{
                    queue.poll();
                    queue.notify();
                    System.out.println("consume an item,the left size is: "+(size-queue.size()));
                }
            }
            // 適用 BlockingQueue 內部已經實現了 線程間的通信機制
//          try{
//              queue.take();
//          }catch(InterruptedException e){
//              e.printStackTrace();
//          }

        }
    }
}

使用ArrayBlockingQueue 實現的生產者消費者模式 更加的簡介,如上面的代碼所示,只需要take,put 內部判斷滿不滿 ,以及內部實現線程間通信機制

發佈了40 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章