阻塞隊列 BlockingQueue

BlockingQueue的四組API
1.運行時會拋異常的
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
/** 第一組:運行是會拋異常的!
* 添加 add() 運行時異常:java.lang.IllegalStateException: Queue full 隊列以滿!;
* 移除 remove() 運行時異常:java.util.NoSuchElementException 隊列已空,無法再進行移除操作!;
* 彈出隊首元素 element()*/
System.out.println(blockingQueue.add("a"));
System.out.println(blockingQueue.add("b"));
System.out.println(blockingQueue.add("b"));
//System.out.println(blockingQueue.add("d"));
System.out.println("===========");
System.out.println(blockingQueue.element());//彈出當前隊首元素。
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
//System.out.println(blockingQueue.remove());
2.返回一個Boolean值不拋異常的
/**
* 第二組:返回一個特征值true/false,不拋異常的
* 添加 offer();
* 移除 poll();
* 彈出當前隊首元素 peek();*/
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.offer("a"));
System.out.println(blockingQueue.offer("b"));
System.out.println(blockingQueue.offer("c"));
//System.out.println(blockingQueue.offer("d"));
System.out.println("===================");
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.peek());//彈出當前隊首元素
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
//System.out.println(blockingQueue.poll());
3.阻塞,等待(一直等?。。。?/p>
/**
* 第三組:阻塞,等待(一直等!)
* 添加 put();
* 移除 take();*/
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
try {
blockingQueue.put("a");
blockingQueue.put("b");
blockingQueue.put("c");
//blockingQueue.put("d");
System.out.println("=============");
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();
}
4.阻塞,等待(設(shè)置等待超時)
/**
* 第四組:阻塞,等待(等待超時)
* 添加 offer(e,TimeOut,unit); e --> 元素 TimeOut --> 超時時間 unit --> 單位
* 移除 poll(TimeOut,unit); TimeOut --> 超時時間 unit --> 單位 */
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
try {
System.out.println(blockingQueue.offer("a"));
System.out.println(blockingQueue.offer("b"));
System.out.println(blockingQueue.offer("c"));
System.out.println(blockingQueue.offer("d", 2, TimeUnit.SECONDS));
System.out.println("========================");
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
}
同步隊列 SynchronousQueue
沒有容量,進去一個元素,必須等待該元素取出來之后,才能再往里面放一個元素。
public class SQDemo {
public static void main(String[] args) {
BlockingQueue<String> blockingQueue = new SynchronousQueue<>();
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+" put 1");
blockingQueue.put("1");
System.out.println(Thread.currentThread().getName()+" put 2");
blockingQueue.put("2");
System.out.println(Thread.currentThread().getName()+" put 3");
blockingQueue.put("3");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName() + " ===> " + blockingQueue.take());
System.out.println(Thread.currentThread().getName() + " ===> " + blockingQueue.take());
System.out.println(Thread.currentThread().getName() + " ===> " + blockingQueue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B").start();
}
}