Java阻塞队列之BlockingQueue

2022-11-02 15:10:07 浏览数 (1)

阻塞队列的概念

  • 当阻塞队列是空时,从队列中获取元素的操作将会被阻塞
  •   当阻塞队列是满时,往队列里添加元素的操作将会被阻塞

ArrayBlockingQueue

代码语言:javascript复制
数组组成的有界阻塞队列

代码示例:add、remove、element(一言不合就报错比较暴力)

代码语言:javascript复制
    public static void main(String[] args) throws InterruptedException {
        // 定义阻塞队列的长度为3
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        // 使用add插入,如果队列已满则会直接报错
        blockingQueue.add(1);
        blockingQueue.add(2);
        blockingQueue.add(3);
        blockingQueue.add(4);
        // 使用element检查队列,如果队列是空则直接报错
        blockingQueue.element();
        // 使用remove删除元素,如果队列是空直接报错
        blockingQueue.remove();
        blockingQueue.remove();
        blockingQueue.remove();
        blockingQueue.remove();
    }

代码示例:offer、poll、peek(比较温和,不会报错只会返回false和null)

代码语言:javascript复制
    public static void main(String[] args) throws InterruptedException {
        // 定义阻塞队列的长度为3
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        // 使用offer插入,如果队列已满则会返回false
        System.out.println(blockingQueue.offer(1));
        System.out.println(blockingQueue.offer(2));
        System.out.println(blockingQueue.offer(3));
        System.out.println(blockingQueue.offer(4));
        // 使用peek检查队列,如果队列是空返回null
        System.out.println(blockingQueue.peek());
        // 使用poll删除元素,如果队列是空返回null
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }

代码示例:put、take(在队列是满的情况下put操作会一直等待空出来位置,在是空的情况下take操作会等待队列有值)

代码语言:javascript复制
    public static void main(String[] args) throws InterruptedException {
        // 定义阻塞队列的长度为3
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        // 使用put插入,如果队列已满则会一直等待队列空出来
        blockingQueue.put(1);
        blockingQueue.put(2);
        blockingQueue.put(3);
        blockingQueue.put(4);
        // 使用take删除元素,如果队列是空会一直等待队列有值
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
    }

代码示例:offer、poll(限时等待,加入了限时秒数,等待x秒后不再阻塞)

代码语言:javascript复制
    public static void main(String[] args) throws InterruptedException {
        // 定义阻塞队列的长度为3
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        // 使用限时offer插入,如果队列已满则会等待1s后退出
        blockingQueue.offer(1,1L,TimeUnit.SECONDS);
        blockingQueue.offer(2,1L,TimeUnit.SECONDS);
        blockingQueue.offer(3,1L,TimeUnit.SECONDS);
        blockingQueue.offer(4,1L,TimeUnit.SECONDS);
        // 使用限时poll删除元素,如果队列是空会等待1s后退出
        System.out.println(blockingQueue.poll(1L,TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(1L,TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(1L,TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(1L,TimeUnit.SECONDS));
    }

SynchronousQueue

代码语言:javascript复制
不存储元素的阻塞队列,只有一个元素

代码示例:

代码语言:javascript复制
    public static void main(String[] args) throws InterruptedException {
        // 定义阻塞队列的长度为3
        BlockingQueue blockingQueue = new SynchronousQueue();

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

        new Thread(()->{
            try {
                Thread.sleep(1000);
                System.out.println(blockingQueue.take());
                Thread.sleep(1000);
                System.out.println(blockingQueue.take());
                Thread.sleep(1000);
                System.out.println(blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

0 人点赞