BlockingQueue
API文档中的描述
BlockingQueue与Collection的关系
四组API
方式 | 抛出异常 | 有返回值,不抛异常 | 阻塞 等待 | 超时等待 |
---|---|---|---|---|
添加 | add() | offer() | put() | offer(,) |
移除 | remove() | poll() | take() | poll(,) |
检测队首元素 | element() | peek() |
添加:
add()只能添加队列的容量大小的元素,如果添加元素数量超过队列容量大小,则会IllegalStateException: Queue full 抛出异常!
public class BlockingQueue {
public static void main(String[] args) throws InterruptedException {
test();
}
/**
* 抛出异常
*/
public static void test(){
// 队列的大小
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.add("a"));
System.out.println(blockingQueue.add("b"));
System.out.println(blockingQueue.add("c"));
System.out.println(blockingQueue.add("d"));
}