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();
    }
}
相关文章
相关标签/搜索