在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. Windows系统下Redis的安装

    Redis是一个用的比较广泛的Key/Value的内存数据库,新浪微博.Github.StackOverflow 等大型应用中都用其作为缓存,Redis的官网为http://redis.io/. 最近 ...

  2. 采用malloc分别分配2KB个人空间,然后,realloc调整到6KB、1MB、3MB、10MB场地,分别这五内存“A”、“B”、“C”、“D”、“E”灌装

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> i ...

  3. Web API-属性路由

    路由(Routing)就是Web API如何将一个URI匹配到一个action的过程.Web API 2 支持一个新的路由方式-属性路由(attribute routing).顾名思义,属性路由使用标 ...

  4. 蓝色的成长记录——追逐DBA(8):为了夺回SP报告,回顾oracle的STATSPACK实验

    ***********************************************声明*************************************************** ...

  5. Asp.net vNext 学习3

    Asp.net vNext 学习之路(三) asp.net vNext 对于构建asp.net 程序带来了一些重大的改变,让我们开发asp.net 程序的时候更加的方便和高效. 1,可以很容易的去管理 ...

  6. ASP.NET 5+EntityFramework 7

    爱与恨的抉择:ASP.NET 5+EntityFramework 7   EF7 的纠缠 ASP.NET 5 的无助 忘不了你的好 一开始列出的这个博文大纲,让我想到了很久之前的一篇博文:恋爱虽易,相 ...

  7. DDD分层架构之值对象(层超类型篇)

    DDD分层架构之值对象(层超类型篇) 上一篇介绍了值对象的基本概念,得到了一些朋友的支持,另外也有一些朋友提出了不同意见.这其实是很自然的事情,设计本来就充满了各种可能性,没有绝对正确的做法,只有更好 ...

  8. !DOCTYPE html文档类型声明简写 HTML5 DOCTYPE缩写

    html5之!DOCTYPE html文档类型声明简写,在HTML5中DOCTYPE简写非常重要. 一.概述   -   TOP 让CSS样式表生效,DOCTYPE声明是必须的,以前TABLE布局的网 ...

  9. .9 png图片的制作

    在android开发的过程中我们经常因为没有好的美工图片失真,这样使界面看起来要逊色很多,有的时候可能我们会想在drawable-hdpi,ldpi,mdpi下放不同分辨率的图片,这样虽然可以有效避免 ...

  10. Nexus入门指南(图文)

    Nexus入门指南(图文) 博客分类: Maven JavamavenGoogleApacheTomcat Nexus介绍 Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven ...