JAVA多线程经典问题 -- 生产者 消费者 同步队列实现方法
在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
有不足之处和错误之处,请留言,本人虚心请教。
JAVA多线程经典问题 -- 生产者 消费者 同步队列实现方法的更多相关文章
- JAVA多线程经典问题 -- 生产者 消费者
工作2年多来一直也没有计划写自己的技术博客,最近辞职在家翻看<thingking in JAVA>,偶尔看到了生产者与消费者的一个经典的多线程同步问题.本人在工作中很少使用到多线程以及高并 ...
- java 多线程 22 :生产者/消费者模式 进阶 利用await()/signal()实现
java多线程15 :wait()和notify() 的生产者/消费者模式 在这一章已经实现了 wait/notify 生产消费模型 利用await()/signal()实现生产者和消费者模型 一样 ...
- Java多线程-并发协作(生产者消费者模型)
对于多线程程序来说,不管任何编程语言,生产者和消费者模型都是最经典的.就像学习每一门编程语言一样,Hello World!都是最经典的例子. 实际上,准确说应该是“生产者-消费者-仓储”模型,离开了仓 ...
- JAVA多线程编程之生产者消费者模式
Java中有一个BlockingQueue可以用来充当堵塞队列,下面是一个桌面搜索的设计 package net.jcip.examples; import java.io.File; import ...
- Java多线程14:生产者/消费者模型
什么是生产者/消费者模型 一种重要的模型,基于等待/通知机制.生产者/消费者模型描述的是有一块缓冲区作为仓库,生产者可将产品放入仓库,消费者可以从仓库中取出产品,生产者/消费者模型关注的是以下几个点: ...
- java 线程 生产者-消费者与队列,任务间使用管道进行输入、输出 解说演示样例 --thinking java4
package org.rui.thread.block2; import java.io.BufferedReader; import java.io.IOException; import jav ...
- java线程基础巩固---多线程下的生产者消费者模型,以及详细介绍notifyAll方法
在上一次[http://www.cnblogs.com/webor2006/p/8419565.html]中演示了多Product多Consumer假死的情况,这次解决假死的情况来实现一个真正的多线程 ...
- Linux 进程间通信(包含一个经典的生产者消费者实例代码)
前言:编写多进程程序时,有时不可避免的需要在多个进程之间传递数据,我们知道,进程的用户的地址空间是独立,父进程中对数据的修改并不会反映到子进程中,但内核是共享的,大多数进程间通信方式都是在内核中建立一 ...
- 使用Java的BlockingQueue实现生产者-消费者
http://tonl.iteye.com/blog/1936391 使用Java的BlockingQueue实现生产者-消费者 博客分类: Java JavaBlockingQueue阻塞队列 B ...
随机推荐
- 【.NET特供-第三季】ASP.NET MVC系列:MVC与三层图形对照
近期在开发小组在研究:BS项目中是利用'MVC框架'还是继续沿用'三层'的问题. 由于曾经的.NET项目大多数都是利用三层开发的,所以大多数人都可以对三层进行熟练地运用.而项目的開始我们也曾听说过MV ...
- 通讯录C++console application
#include<iostream> #include<fstream> #include<string> #include<cstring> #inc ...
- 使用Bootstrap
开始使用Bootstrap 作为一名Web开发者而言,如果不借助任何前端框架,从零开始使用HTML和CSS来构建友好的页面是非常困难的.特别是对于Windows Form的开发者而言,更是难上加难. ...
- C#中实现WebBrowser控件的HTML源代码读写
原文:C#中实现WebBrowser控件的HTML源代码读写 C#中实现WebBrowser控件的HTML源代码读写http://www.blogcn.com/user8/flier_lu/index ...
- Oracle Data Provider for .NET now on NuGet
Oracle Data Provider for .NET now on NuGet 时间 2015-03-02 22:30:00 Oracle Bloggers原文 http://cshay.b ...
- [DevEpxress]GridControl 显示Gif动画
原文:[DevEpxress]GridControl 显示Gif动画 如果没有对进行设置,那么GridControl列中gif在编辑状态下,才能显示动画效果,如果要设置列自动显示动画效果,可以进行如下 ...
- 提高mysql千万级数据SQL的查询优化30条总结
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- WebApp 框架
Razor 在WebApp 框架的运用 前面有两章介绍了WebApp框架<WebApp MVC,“不一样”的轻量级互联网应用程序开发框架>和<WebApp MVC 框架的开发细节 ...
- c++ 正則表達式
正則表達式是经常使用的一种方法.比較有名的类库是boost,可是这个类库在重了.全部就像找一些轻量级的类库. 后来发现准标准的库tr1已经非常方便了,微软vs2008 sp1 以上版本号都支持了.全部 ...
- Singal Page App:使用Knockout和RequireJS创建高度模块化的单页应用引擎
Singal Page App 开篇扯淡 距离上一篇文章已经有好几个月,也不是没有时间记录点东西,主要是换了新的工作,在一家外资工作,目前的工作内容大多都是前端开发,新接触的东西因为时间原因,大多还不 ...