java阻塞队列疑问?

likai22 2008-09-09 10:14:35
请问java阻塞队列的作用是什么?比如LinkBlockingQueue
最好能举个例子说明一下,谢谢
...全文
375 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zerohouse 2010-06-17
  • 打赏
  • 举报
回复
2楼的是api demo,当然写的好了
sunjie310110 2008-10-26
  • 打赏
  • 举报
回复
2楼写的例子不错
likai22 2008-09-09
  • 打赏
  • 举报
回复
结贴,给分!!!!!!!!!
likai22 2008-09-09
  • 打赏
  • 举报
回复
楼上的例子堪称完美,通俗易懂,谢了
宋玮-深圳 2008-09-09
  • 打赏
  • 举报
回复
实现的BlockingQueue接口,描述如下:
* A {@link java.util.Queue} that additionally supports operations
* that wait for the queue to become non-empty when retrieving an
* element, and wait for space to become available in the queue when
* storing an element.
它的实现都是线程同步的。下面是API给的一个生产-消费 例子
Usage example, based on a typical producer-consumer scenario. Note that a BlockingQueue can safely be used with multiple producers and multiple consumers.

class Producer implements Runnable {
private final BlockingQueue queue;
Producer(BlockingQueue q) { queue = q; }
public void run() {
try {
while (true) { queue.put(produce()); }
} catch (InterruptedException ex) { ... handle ...}
}
Object produce() { ... }
}

class Consumer implements Runnable {
private final BlockingQueue queue;
Consumer(BlockingQueue q) { queue = q; }
public void run() {
try {
while (true) { consume(queue.take()); }
} catch (InterruptedException ex) { ... handle ...}
}
void consume(Object x) { ... }
}

class Setup {
void main() {
BlockingQueue q = new SomeQueueImplementation();
Producer p = new Producer(q);
Consumer c1 = new Consumer(q);
Consumer c2 = new Consumer(q);
new Thread(p).start();
new Thread(c1).start();
new Thread(c2).start();
}
}

zjx2388 2008-09-09
  • 打赏
  • 举报
回复
没用过

来学习的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧