在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多线程经典问题 -- 生产者 消费者 同步队列实现方法的更多相关文章

  1. JAVA多线程经典问题 -- 生产者 消费者

    工作2年多来一直也没有计划写自己的技术博客,最近辞职在家翻看<thingking in JAVA>,偶尔看到了生产者与消费者的一个经典的多线程同步问题.本人在工作中很少使用到多线程以及高并 ...

  2. java 多线程 22 :生产者/消费者模式 进阶 利用await()/signal()实现

    java多线程15 :wait()和notify() 的生产者/消费者模式 在这一章已经实现了  wait/notify 生产消费模型 利用await()/signal()实现生产者和消费者模型 一样 ...

  3. Java多线程-并发协作(生产者消费者模型)

    对于多线程程序来说,不管任何编程语言,生产者和消费者模型都是最经典的.就像学习每一门编程语言一样,Hello World!都是最经典的例子. 实际上,准确说应该是“生产者-消费者-仓储”模型,离开了仓 ...

  4. JAVA多线程编程之生产者消费者模式

    Java中有一个BlockingQueue可以用来充当堵塞队列,下面是一个桌面搜索的设计 package net.jcip.examples; import java.io.File; import ...

  5. Java多线程14:生产者/消费者模型

    什么是生产者/消费者模型 一种重要的模型,基于等待/通知机制.生产者/消费者模型描述的是有一块缓冲区作为仓库,生产者可将产品放入仓库,消费者可以从仓库中取出产品,生产者/消费者模型关注的是以下几个点: ...

  6. java 线程 生产者-消费者与队列,任务间使用管道进行输入、输出 解说演示样例 --thinking java4

    package org.rui.thread.block2; import java.io.BufferedReader; import java.io.IOException; import jav ...

  7. java线程基础巩固---多线程下的生产者消费者模型,以及详细介绍notifyAll方法

    在上一次[http://www.cnblogs.com/webor2006/p/8419565.html]中演示了多Product多Consumer假死的情况,这次解决假死的情况来实现一个真正的多线程 ...

  8. Linux 进程间通信(包含一个经典的生产者消费者实例代码)

    前言:编写多进程程序时,有时不可避免的需要在多个进程之间传递数据,我们知道,进程的用户的地址空间是独立,父进程中对数据的修改并不会反映到子进程中,但内核是共享的,大多数进程间通信方式都是在内核中建立一 ...

  9. 使用Java的BlockingQueue实现生产者-消费者

    http://tonl.iteye.com/blog/1936391 使用Java的BlockingQueue实现生产者-消费者 博客分类: Java JavaBlockingQueue阻塞队列  B ...

随机推荐

  1. Mac OS X于Android Kernel下载方法

    于上一篇日志中,我总结了大家提供的下载Android源代码的方法.这里再简单总结一下内核的下载方法. 參考这里的介绍:http://source.android.com/source/building ...

  2. uboot中raise:Signal #8 caught的根本原因

    在移植uboot时编译一切正常,但uboot启动中载入自己写的网卡驱动出现故障,一直在打印raise:Signal #8 caught google  百度了一番,也有非常多人遇到了这个问题,大家都说 ...

  3. 使用C语言编写windows服务一般框架

    原文:使用C语言编写windows服务一般框架 编写windows服务和编写windows应用程序一样,有一些回调函数必须填写且向windows 服务管理器(service manager)进行注册, ...

  4. linux下mysql的远程连接

    在服务器上安装mysql后,想使用本地的mysql客户端连接数据库时,提示不允许连接,比较郁闷,找到了这篇文章解决了我的问题: 内容如下: 本地计算机ip:192.168.1.100远程计算机ip:1 ...

  5. 使用C/C++发展Web系统开源

    下载 见 C++开发的论坛系统 - BBS 下载地址:Fetch_source_code_release_vse2008_v1.2.1.7z 眼下先暂存在百度云上,最近会放入github 当前版本号的 ...

  6. sqlserver大容量日志文件处理

    原文:sqlserver大容量日志文件处理 针对SqlServer2000 .SqlServer2005.SqlServer2008.SqlServer2012.SqlServer2014库日志文件优 ...

  7. C语言运算符表(优先级)

    http://www.is.pku.edu.cn/~qzy/c/operator.htm

  8. Solr多核心及分词器(IK)配置

    Solr多核心及分词器(IK)配置   多核心的概念 多核心说白了就是多索引库.也可以理解为多个"数据库表" 说一下使用multicore的真实场景,比若说,产品搜索和会员信息搜索 ...

  9. .NET MVC4 实训记录之三(EntityFramework 与枚举)

    EntityFramework对枚举的引入是从版本5开始的(如果没有记错的话).枚举可以很大程度上提高对程序的可读性.那么在EntityFramework的CodeFirst模式下,如何使用枚举呢?答 ...

  10. 大规模web服务开发技术

    大规模web服务开发技术 总评        这本书是日本一个叫hatena的大型网站的CTO写的,通过hatena网站从小到大的演进来反应一个web系统从小到大过程中的各种系统和技术架构变迁,比较接 ...