阻塞隊(duì)列 BlockingQueue
BlockingQueue的四組API
1.運(yùn)行時(shí)會(huì)拋異常的
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
/** 第一組:運(yùn)行是會(huì)拋異常的雌桑!
* 添加 add() 運(yùn)行時(shí)異常:java.lang.IllegalStateException: Queue full 隊(duì)列以滿腕让!;
* 移除 remove() 運(yùn)行時(shí)異常:java.util.NoSuchElementException 隊(duì)列已空裹纳,無(wú)法再進(jìn)行移除操作挪拟!;
* 彈出隊(duì)首元素 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());//彈出當(dāng)前隊(duì)首元素挨务。
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
//System.out.println(blockingQueue.remove());
2.返回一個(gè)Boolean值不拋異常的
/**
* 第二組:返回一個(gè)特征值true/false,不拋異常的
* 添加 offer();
* 移除 poll();
* 彈出當(dāng)前隊(duì)首元素 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());//彈出當(dāng)前隊(duì)首元素
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
//System.out.println(blockingQueue.poll());
3.阻塞,等待(一直等!;驯6≈丁)
/**
* 第三組:阻塞,等待(一直等3住)
* 添加 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è)置等待超時(shí))
/**
* 第四組:阻塞,等待(等待超時(shí))
* 添加 offer(e,TimeOut,unit); e --> 元素 TimeOut --> 超時(shí)時(shí)間 unit --> 單位
* 移除 poll(TimeOut,unit); TimeOut --> 超時(shí)時(shí)間 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();
}
同步隊(duì)列 SynchronousQueue
沒有容量劈猿,進(jìn)去一個(gè)元素拙吉,必須等待該元素取出來(lái)之后,才能再往里面放一個(gè)元素揪荣。
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();
}
}