在JAVASE5 中的java.util.concurrent.BlockingQueue支持,BlockingQueue是一个接口但是我们通常可以使用LinkedBlockingQueue,它是一个无界的队列,当然我们还可以使用ArrayBlockingQueue,它拥有固定的尺寸,因此我们可以在他被阻塞之前放入有限的元素。

当消费者试图从队列中获取对象时,如果队列为空,那么这些队列还可以挂起消费者任务,多么神奇的功能,那么当队列中有足够的元素可以供消费者获取,那么他可以回复消费者任务,比使用一些让人难理解的notifyAll wait要简单,并且可靠很多。简单写了两句

class Product {
private final int orderNum; public Product(int orderNum) {
this.orderNum = orderNum;
} public String toString() {
return "Product" + orderNum;
}
} class Producter implements Runnable {
private MainQueue main; public Producter(MainQueue main) {
this.main = main;
} public void run() {
Product product = new Product(new Random().nextInt(100));
//向队列插入一个元素 ,此时consumer任务是获取不了当前这个队列的所即他读取不了里面的数据
main.queue.add(product);
System.out.println("the Producter put the " + product
+ " in to the queue");
}
} class Consumer implements Runnable {
private MainQueue main;
public Consumer(MainQueue main) {
this.main = main;
}
public void run() {
while (main.queue.size() > 0) {
Product product = null;
try {
//读队列中的一个元素,此时product任务写不进去元素
product = main.queue.take();
System.out.println("the Consumer get the" + product
+ " from the quene");
} catch (InterruptedException e) {
System.out.println("Consumer interrupted!");
}
}
} } public class MainQueue {
//这是一个同步队列 它只允许一个任务插入或者删除元素 LinkedBlockingQeque是一个无界的队列
BlockingQueue<Product> queue = new LinkedBlockingDeque<>();
Producter producter = new Producter(this);
Consumer consumer = new Consumer(this); public MainQueue() {
for (int i = 0; i < 10; i++) {
new Thread(producter).start();
}
for (int i = 0; i < 10; i++) {
new Thread(consumer).start();
}
} public static void main(String[] args) {
new MainQueue();
}
}

看是不是很简单,运行结果如下:

the Producter put the Product91 in to the queue
the Producter put the Product50 in to the queue
the Producter put the Product72 in to the queue
the Producter put the Product46 in to the queue
the Producter put the Product92 in to the queue
the Producter put the Product91 in to the queue
the Producter put the Product52 in to the queue
the Producter put the Product48 in to the queue
the Producter put the Product41 in to the queue
the Consumer get theProduct91 from the quene
the Consumer get theProduct52 from the quene
the Producter put the Product72 in to the queue
the Consumer get theProduct92 from the quene
the Consumer get theProduct50 from the quene
the Consumer get theProduct72 from the quene
the Consumer get theProduct72 from the quene
the Consumer get theProduct91 from the quene
the Consumer get theProduct48 from the quene
the Consumer get theProduct41 from the quene
the Consumer get theProduct46 from the quene

有不足之处和错误之处,请留言,本人虚心请教。

最新文章

  1. Scala Reflection - Mirrors,ClassTag,TypeTag and WeakTypeTag
  2. contiki-rtimer
  3. Redis ConnectionException
  4. pdflatex, xelatex, texstudio中文编码问题
  5. asp.net 前台绑定后台变量方法总结:&lt;%= %&gt; 和&lt;%# %&gt;的区别
  6. SPI通信实验---verilog(FPGA作为从机,使用可读可写)
  7. magic矩阵 分类: 数学 2015-07-31 22:56 2人阅读 评论(0) 收藏
  8. url中文参数解决方案
  9. 开源web终端ssh解决方案-gateone简介
  10. ARC小知识
  11. asp.net:repeater嵌套(常用于新闻等在首页归类显示)
  12. .net对象转Datable
  13. url 的正则表达式:path-to-regexp
  14. DCOS实践分享(6):基于DCOS的大数据应用分享
  15. SoapUI 学习总结-02 断言
  16. ML_Clustering
  17. XML报错:The reference to entity &quot;characterEncoding&quot; must end with the &#39;;&#39; delimite
  18. N76E003之IIC
  19. 错误代码0x00000001,好多软件连不了网,求助~(WIN7/win8/win9/win10)
  20. LeetCode--100--相同的树

热门文章

  1. IOS中 类扩展 xib
  2. Enum:枚举
  3. Pku1218
  4. Android Material Design带UI变化
  5. 第3章2节《MonkeyRunner源码剖析》脚本编写示例: MonkeyDevice API使用示例(原创)
  6. hdu oj1102 Constructing Roads(最小生成树)
  7. SQL点滴4—筛选数据列的类型,字段大小,是否可为空,是否是主键,约束等等信息
  8. openwrt固件支持3G和4G上网卡
  9. Visual Studio 2015 &amp; C#6.0
  10. MySQL之查询优化方式(笔记)