blockingqueue學習筆記

一。接口blockingqueue安全

      1>BlockingQueue是一種特殊的queue,特殊性以下:
      若blockingQueue是空的,從該隊列中取東西的操做將會被阻斷進入等待狀態,知道blockingqueue進了新貨纔會被喚醒。
      若blockingQueue是滿的,向該隊列中村東西的操做也會被阻斷進入等待狀態,直到blockingqueue有了空間纔會被喚醒
     2>BlockingQueue主要有如下方法app

     添加類方法:
    add(Object)方法:若是沒滿返回true,若是滿了拋出illegalStateException
     offer(Object)方法:若是沒滿返回true,若是滿了返回false
     put(Object)方法:若是沒滿返回true,若是滿了阻塞直到有新空間
    拿取類方法:
    poll(time)方法:若是爲空阻塞,若是一直爲空time時間後返回null
    take()方法:若是爲空一直阻塞到不爲空ide

二。BlockingDeque性能

      deque通常是雙端隊列,會比queue多addFirst、addLast、等方法,具體方法參考queue。spa

三。BlockingQueue和BLockingDeque的幾種實現線程

    1>ArrayBlockingQueuecode

          是線程安全的,構造器必須帶長度,能夠規定訪問策略是否爲FIFO(先入先出) 默認就是FIFO的,fair參數若是是false的話,先入不能保證,因此先出也沒用了,緣由是在競爭鎖時,不能保證順序。排序

    2>LinkedBlockingQueue接口

           線程安全的,構造器能夠不定義大小,也能夠定義。若不帶大小,最大爲Integer.MAX_VALUE決定隊列

    1>、2>相比,2的數據吞吐量要大,但在線程數量很大是,其性能的可預見性要低於1

    3>PriorityBlockingQueue。

            它最大的特色是

            能夠本身定義一個比較器,個性化爲隊列的順序排序。用法很簡單。

    4>SynchronousQueue

            這個queue比較特殊,對其操做必須是放和取交替完成的。用到再研究

 

四。用BlockingQueue來實現Producer和Consumer的例子,代碼以下:

 

public class BlockingQueueTest {
    public static class Basket {
        BlockingQueue<String> basket = new ArrayBlockingQueue<String>(3);
        public void produce() throws InterruptedException {
            basket.put("An apple");
        }
        public int getSize(){
            return basket.size();
        }
        public String consume() throws InterruptedException {
            return basket.take();
        }
    }

    public static void testBasket() {
        final Basket basket = new Basket();
        class Consumer implements Runnable {

            @Override
            public void run() {
                try {
                    while (true) {
                        System.out.println("消費蘋果"+(basket.getSize()+1)+"中");
                        basket.consume();
                        System.out.println("消費"+basket.getSize()+"蘋果");
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        class Producer implements Runnable {


            @Override
            public void run() {
                try {
                    while (true) {
                        System.out.println("生產"+(basket.getSize()+1)+"蘋果中");
                        basket.produce();
                        System.out.println("生產"+basket.getSize()+"蘋果");
                        Thread.sleep(300);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        ExecutorService service=Executors.newCachedThreadPool();
        Producer producer=new Producer();
        Consumer consumer=new Consumer();
        service.submit(producer);
        service.submit(consumer);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        service.shutdownNow();
    }
    public static void main(String[] args) {
        testBasket();
    }
}
相關文章
相關標籤/搜索