方法对比
|
抛出异常 |
特殊值 |
阻塞 |
超时 |
插入 |
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();
}
}
}