阻塞队列

阻塞队列 BlockingQueue

BlockingQueue的四组API

1.运行时会抛异常的

BlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        /** 第一组:运行是会抛异常的!
         * 添加 add() 运行时异常:java.lang.IllegalStateException: Queue full 队列以满!;
         * 移除 remove() 运行时异常:java.util.NoSuchElementException 队列已空,无法再进行移除操作!;
         * 弹出队首元素 element()*/
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("b"));
        //System.out.println(blockingQueue.add("d"));

        System.out.println("===========");
        System.out.println(blockingQueue.element());//弹出当前队首元素。
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        //System.out.println(blockingQueue.remove());

2.返回一个Boolean值不抛异常的

/**
         * 第二组:返回一个特征值true/false,不抛异常的
         * 添加 offer();
         * 移除 poll();
         * 弹出当前队首元素 peek();*/
        BlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        //System.out.println(blockingQueue.offer("d"));

        System.out.println("===================");

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.peek());//弹出当前队首元素
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //System.out.println(blockingQueue.poll());

3.阻塞,等待(一直等!!!)

/**
         * 第三组:阻塞,等待(一直等!)
         * 添加 put();
         * 移除 take();*/
        BlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        try {
            blockingQueue.put("a");
            blockingQueue.put("b");
            blockingQueue.put("c");
            //blockingQueue.put("d");
            System.out.println("=============");
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());
            System.out.println(blockingQueue.take());
            //System.out.println(blockingQueue.take());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

4.阻塞,等待(设置等待超时)

/**
         * 第四组:阻塞,等待(等待超时)
         * 添加 offer(e,TimeOut,unit); e --> 元素  TimeOut --> 超时时间  unit --> 单位
         * 移除 poll(TimeOut,unit); TimeOut --> 超时时间  unit --> 单位 */
        BlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        try {
            System.out.println(blockingQueue.offer("a"));
            System.out.println(blockingQueue.offer("b"));
            System.out.println(blockingQueue.offer("c"));
            System.out.println(blockingQueue.offer("d", 2, TimeUnit.SECONDS));

            System.out.println("========================");

            System.out.println(blockingQueue.poll());
            System.out.println(blockingQueue.poll());
            System.out.println(blockingQueue.poll());
            System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

同步队列 SynchronousQueue

没有容量,进去一个元素,必须等待该元素取出来之后,才能再往里面放一个元素。

public class SQDemo {
    public static void main(String[] args) {
        BlockingQueue blockingQueue = new SynchronousQueue<>();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+" put 1");
                blockingQueue.put("1");
                System.out.println(Thread.currentThread().getName()+" put 2");
                blockingQueue.put("2");
                System.out.println(Thread.currentThread().getName()+" put 3");
                blockingQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + " ===> " + blockingQueue.take());
                System.out.println(Thread.currentThread().getName() + " ===> " + blockingQueue.take());
                System.out.println(Thread.currentThread().getName() + " ===> " + blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B").start();

    }
}

你可能感兴趣的:(阻塞队列)