在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. 剑指XX(游戏10) - 走正步工厂一个安静的农场游戏的代码

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2lsYW5ncXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  2. visual studio 辅助工具

    resharper  这是一个收费软件 需要下载对应版本的注册机. 效果如图所示: 这里的using 很多没有用到,他会用灰色标记,你都可以统统去掉. 声明一个类 ,要求 首字母大写,如果你小写了,他 ...

  3. JS 数组array方法push, pop, unshift, shift, slice,splice,contact, join, sort

    Array:数组对象用来在单独的变量名中存储一系列的值   定义数组:         1. var arrayObj = new Array();         2. var arrayObj = ...

  4. 如何将经纬度利用Google Map API显示C# VS2005 Sample Code

    原文 如何将经纬度利用Google Map API显示C# VS2005 Sample Code 日前写了一篇如何用GPS抓取目前所在,并回传至资料库储存,这篇将会利用这些回报的资料,将它显示在地图上 ...

  5. angular.js的一点理解

    对angular.js的一点理解 2015-01-14 13:18 by MrGeorgeZhao, 317 阅读, 4 评论, 收藏, 编辑 最近一直在学习angular.js.不得不说和jquer ...

  6. mongodb操作之使用javaScript实现多表关联查询

    一.数据控制 mongodb操作数据量控制,千万控制好,不要因为操作的数据量过多而导致失败. 演示一下发生此类错误的错误提示:

  7. Goldeneye.py网站压力测试工具2.1版源码

    Goldeneye压力测试工具的源代码,粗略看了下,代码写的蛮规范和易读的,打算边读边加上了中文注释,但是想来也没太大必要,代码600多行,值得学习的地方还是蛮多的,喜欢Python的同学可以一读 这 ...

  8. iPhone、iPad、iPadMini界面设计标准

    一个:iPhone 4.0' Display: iPhone 5.iPhone 5S.iPhone 5C. 解析度:1136 * 960 设计标准參照下图iPhone5 3.5' Display:   ...

  9. 如何防范CC攻击

    服务器如何防范CC攻击CC攻击是DDOS(分布式拒绝服务)的一种,相比其它的DDOS攻击CC似乎更有技术含量一些.这种攻击你见不到虚假IP,见不到特别大的异常流量,但造成服务器无法进行正常连接,听说一 ...

  10. Ibatis配置存储过程xml文件案例

    -- <parameterMaps> <!--注意:parameterMap中的参数个数和顺序要和ProcGetPersonByName存储过程中的一致--> <para ...