9. 阻塞队列

BlockingQueue

  • 使用场景:多并发处理,线程池

阻塞队列的使用

四组API

方式 抛出异常 有返回值,不抛异常 阻塞等待 超时等待
添加 add offer put offer( , , )
删除 remove poll take poll( , )
检测队列首部 element peek

代码测试

package pers.vincent.matrix.subject.bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 阻塞队列
 */
public class BlockingQueueTest {

    public static void main(String[] args) throws InterruptedException {
        test4();
    }

    /**
     * add / remove 抛出异常
     * 检测队列首部: element
     */
    public static void test1(){
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(4);

        blockingQueue.add("a");
        blockingQueue.add("b");
        blockingQueue.add("c");
        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());
    }

    /**
     * offer / poll 不抛异常
     * peek 检索队列首部
     */
    public static void test2(){
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(4);

        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
        blockingQueue.offer("d");

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

        System.out.println(blockingQueue.peek());

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }

    /**
     * put / take 阻塞
     *
     * @throws InterruptedException
     */
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(4);

        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");

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

        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
    }

    public static void test4() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);

        blockingQueue.offer("a", 2, TimeUnit.SECONDS);
        blockingQueue.offer("b", 2, TimeUnit.SECONDS);
        blockingQueue.offer("c", 2, TimeUnit.SECONDS);

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

        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
    }
}

SynchronousQueue 同步队列

SynchronousQueue 不存储元素,只要往里面put了就必须得take之后才能再put值进去

posted @ 2020-08-07 11:02  湘北不会输的  阅读(61)  评论(0编辑  收藏  举报