ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

20220210 java.util.concurrent.BlockingQueue 方法说明

2022-02-10 21:33:22  阅读:214  来源: 互联网

标签:java 20220210 System remove InterruptedException util println out blockingQueue


方法对比

抛出异常 特殊值 阻塞 超时
插入 add(e) offer(e) put(e) offer(e, time, unit)
移除 remove() poll() take() poll(time, unit)
检查 element() peek() 不可用 不可用

方法声明

// 添加元素方法
boolean add(E e);
boolean offer(E e);
boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;
void put(E e) throws InterruptedException;


// 移除元素方法
boolean remove(Object o);
E remove();
E poll();
E poll(long timeout, TimeUnit unit) throws InterruptedException;
E take() throws InterruptedException;


// 检查元素方法
E element();
E peek();
  • 注意方法的参数、返回值和抛出异常
  • remove 方法有两个重载方法,一个有入参,返回 boolean ;另一个没有入参,返回队列里的元素
  • 方法声明上有抛出 InterruptedException 异常的方法,意思不是指超时后抛出此异常,而是指接收到中断信号后抛出此异常

代码示例

public class BlockingQueueDemo {

    private static BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

    public static void main(String[] args) {

        // add、offer、put
        // testAdd();
        // testOffer();
        // testPut();

        // remove、poll、take
        // testRemove();
        // testRemoveNoArg();
        // testPoll();
        // testTake();

        // element、peek
        // testElement();
        // testPeek();

        // 测试中断异常
        testInterruptedException();


        System.out.println(blockingQueue);
    }

    private static void testInterruptedException() {
        Thread thread = new Thread(() -> {
            try {
                blockingQueue.put("a");
                System.out.println("添加成功 a");
                blockingQueue.put("b");
                System.out.println("添加成功 b");
                blockingQueue.put("c");
                System.out.println("添加成功 c");
                blockingQueue.put("d");
                System.out.println("添加成功 d");
            } catch (InterruptedException e) {
                System.out.println("抛出异常");
                System.err.println(e);  // java.lang.InterruptedException
                e.printStackTrace();    // java.lang.InterruptedException
            }
        });
        thread.start();

        try {
            TimeUnit.SECONDS.sleep(5);
            thread.interrupt();
            System.out.println("线程中断");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void testElement() {
        System.out.println(blockingQueue.element());    // a    // 阻塞队列为空时,抛出异常 java.util.NoSuchElementException
        System.out.println(blockingQueue.element());    // a
        System.out.println(blockingQueue.element());    // a
    }

    private static void testPeek() {
        System.out.println(blockingQueue.peek());    // a    // 阻塞队列为空时,返回 null
        System.out.println(blockingQueue.peek());    // a
        System.out.println(blockingQueue.peek());    // a
    }

    private static void testRemove() {
        blockingQueue.remove("a");
        blockingQueue.remove("b");
        System.out.println(blockingQueue.remove("c")); // true
        System.out.println(blockingQueue.remove("d")); // false
    }

    private static void testRemoveNoArg() {
        System.out.println(blockingQueue.remove()); // a
        System.out.println(blockingQueue.remove()); // b
        System.out.println(blockingQueue.remove()); // c
        System.out.println(blockingQueue.remove()); // java.util.NoSuchElementException
    }

    private static void testPoll() {
        System.out.println(blockingQueue.poll());   // a
        System.out.println(blockingQueue.poll());   // b
        System.out.println(blockingQueue.poll()); // c

        System.out.println(blockingQueue.poll()); // null

        try {
            System.out.println(blockingQueue.poll(5, TimeUnit.SECONDS)); // null    // 阻塞5s
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void testTake() {
        try {
            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();
        }
    }

    private static void testAdd() {
        blockingQueue.add("a");
        blockingQueue.add("b");
        System.out.println(blockingQueue.add("c")); // true
        // System.out.println(blockingQueue.add("d")); // java.lang.IllegalStateException: Queue full
    }

    private static void testOffer() {
        blockingQueue.offer("a");
        blockingQueue.offer("b");
        System.out.println(blockingQueue.offer("c")); // true

        System.out.println(blockingQueue.offer("d")); // false

        try {
            System.out.println(blockingQueue.offer("e", 5, TimeUnit.SECONDS)); // false     // 阻塞5s
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void testPut() {
        try {
            blockingQueue.put("a");
            System.out.println("插入完成 a");
            blockingQueue.put("b");
            System.out.println("插入完成 b");
            blockingQueue.put("c");
            System.out.println("插入完成 c");
            blockingQueue.put("d");     // 线程阻塞
            System.out.println("插入完成 d");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

标签:java,20220210,System,remove,InterruptedException,util,println,out,blockingQueue
来源: https://www.cnblogs.com/huangwenjie/p/15881120.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有