BlockingQueue阻塞隊列
BlockingQueue簡介
juc包下,BlockingQueue很好的解決了多線程中,高效安全的"傳輸數(shù)據(jù)"問題。
阻塞隊列,是一個隊列,可以是數(shù)據(jù)從隊列的一端輸入,從另一端輸出。
當(dāng)隊列空時,從隊列獲取元素線程被阻塞,直到其他線程向空的隊列插入新元素。
當(dāng)隊列滿時,向隊列添加元素線程被阻塞,直到其他線程從隊列中移除一個或多個元素或者完全清空,使隊列變得空閑起來后新增。
在多線程中,阻塞是指在某些情況下,掛起線程,一旦條件滿足,被掛起的線程被自動喚醒。
隊列和棧
隊列 FIFO(先進先出)
棧 FILO(先進后出)
常用的BlockingQueue子類
ArrayBlockingQueue(常用)
由數(shù)組結(jié)構(gòu)組成的有界阻塞隊列。
LinkedBlockingQueue(常用)
由鏈表結(jié)構(gòu)組成的有界(大小默認(rèn)值為Integer.MAX_VALUE)阻塞隊列。
DelayQueue
使用優(yōu)先級隊列實現(xiàn)的延遲無界阻塞隊列。
PriorityBlockingQueue
支持優(yōu)先級排序的無界阻塞隊列。
SynchronousQueue
該隊列在創(chuàng)建時,有兩種模式,公平和非公平。
公平模式會采用公平鎖,并使用隊列來阻塞多余的生成者和消費者,從而實現(xiàn)整體的公平策略。
非公平模式,采用非公平鎖,且使用棧來阻塞多余的生產(chǎn)者和消費者,這種模式下,如果生產(chǎn)者和消費者的處理速度存在差距,則很容易出現(xiàn)饑渴的情況,即可能出現(xiàn)生產(chǎn)不足(消費者在等待生產(chǎn)者),或者生產(chǎn)過剩(有些元素,永遠(yuǎn)不會被消費)。
總結(jié),不存儲元素的同步隊列,即存儲單個元素的對列。
LinkedBlockingDeque
由鏈表組成的雙休阻塞隊列。
BlockingQueue核心方法
文章來源:http://www.zghlxwxcb.cn/news/detail-747389.html
方法使用代碼
/**
* @author 長名06
* @version 1.0
*/
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
//第一組
//add方法無空間可添加元素,拋IllegalStateException("Queue full")異常 有空間可添加元素,返回true
// System.out.println(blockingQueue.add("wind-風(fēng)"));//
// System.out.println(blockingQueue.add("flower-花"));
// System.out.println(blockingQueue.add("snow-雪"));
// System.out.println(blockingQueue.add("moon-月"));
//System.out.println(blockingQueue.element());
//remove方法,無元素可用,拋NoSuchElementException異常 有元素可用,返回元素
// System.out.println(blockingQueue.remove());
// System.out.println(blockingQueue.remove());
// System.out.println(blockingQueue.remove());
// System.out.println(blockingQueue.remove());
//第二組
//offer(E e)方法 無空間可添加元素,返回false 有空間可用添加元素,返回true
//還存在offer(E e)的重載方法offer(E e, long timeout, TimeUnit unit) 對應(yīng)第四組
//無空間可添加元素,會等待一段時間,看是否有空間可用添加元素,等待時間內(nèi)有,添加 沒有就返回false
//有空間可用添加元素,返回true
// System.out.println(blockingQueue.offer("wind-風(fēng)"));
// System.out.println(blockingQueue.offer("flower-花"));
// System.out.println(blockingQueue.offer("snow-雪"));
// System.out.println(blockingQueue.offer("moon-月"));
//
//poll方法,無元素可用,返回null 有元素可用,返回元素
// System.out.println(blockingQueue.poll());
// System.out.println(blockingQueue.poll());
// System.out.println(blockingQueue.poll());
// System.out.println(blockingQueue.poll());
//第三組
//put方法注釋
//Inserts the specified element into this queue, waiting if necessary for space to become available.
//put方法,如果隊列已滿,就一直等待,直到隊列可用添加元素 put方法無返回值
blockingQueue.put("wind-風(fēng)");
blockingQueue.put("flower-花");
blockingQueue.put("snow-雪");
System.out.println("隊列已滿");
System.out.println(blockingQueue.offer("moon-月", 3L, TimeUnit.SECONDS));
// blockingQueue.put("moon-月");
//take方法注釋
//Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
//take方法,不為空返回元素 如果隊列為空,就一直阻塞,等待隊列中有元素可用
// System.out.println(blockingQueue.take());
// System.out.println(blockingQueue.take());
// System.out.println(blockingQueue.take());
// System.out.println("隊列已空");
// blockingQueue.take();
//第四組
//poll方法注釋
//Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
//poll方法,取元素,如果隊列不為空,返回元素
// 如果隊列為空,會等待一段時間,看是否有元素可用,沒有就返回null
System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));
System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));
System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));
// System.out.println(blockingQueue.poll(3L, TimeUnit.SECONDS));
}
}
只是為了記錄自己的學(xué)習(xí)歷程,且本人水平有限,不對之處,請指正。文章來源地址http://www.zghlxwxcb.cn/news/detail-747389.html
到了這里,關(guān)于BlockingQueue阻塞隊列的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!