码迷,mamicode.com
首页 > Windows程序 > 详细

阻塞队列——四组API

时间:2021-03-01 13:23:52      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:off   out   https   初始   抛出异常   element   system   queue   返回   

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

第一组 1、添加抛出异常 ```` public class Demo01 { public static void main(String[] args) { // 构造方法参数:public ArrayBlockingQueue(int capacity, boolean fair) // 第一个参数:初始化阻塞队列的容量大小 // 第二个参数:指定该阻塞队列是否为公平或不公平锁
    BlockingQueue<String> queue = new ArrayBlockingQueue<>(2);

    // 添加元素
    queue.add("a");
    queue.add("b");
    queue.add("c");
}

}

运行结果:
![](https://img2020.cnblogs.com/blog/1688269/202102/1688269-20210228090948807-837242888.png)

2、移除抛出异常

public class Demo01 {
public static void main(String[] args) {
// 构造方法参数:public ArrayBlockingQueue(int capacity, boolean fair)
// 第一个参数:初始化阻塞队列的容量大小
// 第二个参数:指定该阻塞队列是否为公平或不公平锁

    BlockingQueue<String> queue = new ArrayBlockingQueue<>(2);

    // 添加元素
    System.out.println(queue.add("a"));
    System.out.println(queue.add("b"));

    // 移除元素
    queue.remove();
    queue.remove();
    queue.remove();
}

}

运行结果:
![](https://img2020.cnblogs.com/blog/1688269/202102/1688269-20210228091327955-1249491689.png)

阻塞队列——四组API

标签:off   out   https   初始   抛出异常   element   system   queue   返回   

原文地址:https://www.cnblogs.com/pengsay/p/14458126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!