關於Java中Queue和BlockingQueue的區別
隊列是一種特殊的線性表,它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作。進行插入操作的端稱為隊尾,進行刪除操作的端稱為隊頭。隊列中沒有元素時,稱為空隊列。下面小編準備了關於Java中Queue和BlockingQueue的區別,提供給大家參考!
kingQueue:支持兩個附加操作的 Queue,這兩個操作是:檢索元素時等待隊列變為非空,以及存儲元素時等待空間變得可用。
kingQueue 不接受 null 元素。
kingQueue 可以是限定容量的'。
kingQueue 實現是線程安全的。Queue不是線程安全的。因此可以將Blockingqueue用於用於生產者-使用者隊列。
根據Api修改的一個例子,大家可以修改自己體驗BlockingQueue的使用
package test;
import yBlockingQueue;
import kingQueue;
/**
* 生產者
*/
class Producer implements Runnable {
private final BlockingQueue queue;
Producer(BlockingQueue q) {
queue = q;
}
public void run() {
try {
for (int i = 0; i < 3; i++) {
(produce());
tln("生產後:"+());
}
} catch (InterruptedException ex) {
tStackTrace();
}
}
Character produce() {
char c = (char) (om() * 26 + 'A');
tln("生產前:" + c);
return c;
}
}
/**
* 消費者
*/
class Consumer implements Runnable {
private final BlockingQueue queue;
Consumer(BlockingQueue q) {
queue = q;
}
public void run() {
try {
while (true) {
consume(());
//p(100);
}
} catch (InterruptedException ex) {
tStackTrace();
}
}
void consume(Character c) {
tln("消費:" + c);
}
}
/**
* 一個生產者、兩個消費者
*
*/
class Setup {
public static void main(String[] args) {
BlockingQueue q = new ArrayBlockingQueue(1);
Producer p = new Producer(q);
Consumer c1 = new Consumer(q);
Consumer c2 = new Consumer(q);
new Thread(p)t();
new Thread(c1)t();
new Thread(c2)t();
}
}
–EOF–
相關文章
-
java的String=a; a==null和a.equals(null)這兩個判斷有什麼區別
1、String 的==與equal()在對字符串的相等判斷,==判斷的是地址是否相同,equal()判斷的是字符值是否相同。大多數時候==跟equal()的結果都是相同的。這是因為String對象是不變模式的,如果你不是明確地new一個String對象,Ja -
Java中break、continue、return語句的使用區別對比
在軟件開發過程中,邏輯清晰是非常之重要的。 代碼的規範也是非常重要的。往往細節決定成敗。在編寫代碼的時候,一定要理解語言的作用以及使用的方法和場景。你知道Java中break、continue、retu語句的使用區別是什麼嗎? -
Java中break、continue、return語句的使用區別對比介紹
這篇文章主要介紹了Java中break、continue、retu語句的使用區別對比,本文用非常清爽簡明的語言總結了這三個關鍵字的使用技巧,並用一個實例對比使用結果,需要的朋友可以參考下break、continue、retu之間的區別與聯繫 -
Unit 9 A Have you ever been to a museum?八年級英語説課稿
在教學工作者開展教學活動前,總歸要編寫説課稿,寫説課稿能有效幫助我們總結和提升講課技巧。如何把説課稿做到重點突出呢?下面是小編為大家收集的Unit 9 A Have you ever been to a museum?八年級英語説課稿,供大家參考借 -
Perseverance will make you successful英語作文
As is known to all: success lies in perseverance. nothing can make people much more excitation than success. but you must suffer all kinds of hardships throughout your life before you succeed. everyon -
Chinas Liu to seek injury advice in US soon英語作文
hinas world 110m hurdles champion liu iang works out at a training session in inzhuang, shanghai, september 24, .[osports]the 25-year-old athens gold medallist will consult medical eperts in three cit -
linux中pagbuffer和cache的區別
Page cache和buffer cache一直以來是兩個比較容易混淆的概念,在網上也有很多人在爭辯和猜想這兩個cache到底有什麼區別,討論到最後也一直沒有一個統一和正確的結論,在我工作的這一段時間,page cache和buffer cache的概念 -
最新的美文欣賞:Why American Culture is Unique
American culture is unique because it is nurtured, formed and developed under certain conditions, which are characteristically(特性)American. The major factors contributing to the making of this new n -
英語滿分作文:Reading-TheBestMeanstoAcquireknowledge
Reading- The Best Means to Acquire knowledgeKnowledge may be acquired through many ways。 One way of getting knowledge is from travelling Another way is by conversation, especially one with a great man -
英語的美文:Why American Culture is Unique
American culture is unique because it is nurtured, formed and developed under certain conditions, which are characteristically(特性)American. The major factors contributing to the making of this new n