关于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