# 代码:

public class App {

    public static void main(String[] args) {

        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(6);
Producer producer = new Producer(queue);
Consumer consumer = new Consumer(queue); producer.produce(5);
consumer.consume(4);
producer.produce(3);
consumer.consume(2);
}
} class Producer {
private BlockingQueue<Integer> queue; public Producer(BlockingQueue<Integer> queue) {
this.queue = queue;
} public void produce(final int val) {
new Thread() {
public void run() {
try {
int surplus = val;
while (surplus > 0) {
queue.put(1024);
surplus--;
System.out.printf("%s plan to produce (%d), actually produce (%d), queue size (%d) \n",
Thread.currentThread().getName(), val, 1, queue.size());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
} class Consumer {
private BlockingQueue<Integer> queue; public Consumer(BlockingQueue<Integer> queue) {
this.queue = queue;
} public void consume(final int val) {
new Thread() {
public void run() {
int surplus = val;
try {
while (surplus > 0) {
queue.take();
surplus--;
System.out.printf("%s plan to consume (%d), actually consume (%d), queue size (%d) \n",
Thread.currentThread().getName(), val, 1, queue.size());
}
} catch (InterruptedException e) {
e.printStackTrace();
} }
}.start();
}
}

# 输出:

Thread-0 plan to produce (5), actually produce (1), queue size (1)
Thread-3 plan to consume (2), actually consume (1), queue size (0)
Thread-2 plan to produce (3), actually produce (1), queue size (1)
Thread-1 plan to consume (4), actually consume (1), queue size (0)
Thread-2 plan to produce (3), actually produce (1), queue size (1)
Thread-3 plan to consume (2), actually consume (1), queue size (0)
Thread-0 plan to produce (5), actually produce (1), queue size (1)
Thread-2 plan to produce (3), actually produce (1), queue size (1)
Thread-1 plan to consume (4), actually consume (1), queue size (0)
Thread-0 plan to produce (5), actually produce (1), queue size (2)
Thread-1 plan to consume (4), actually consume (1), queue size (1)
Thread-1 plan to consume (4), actually consume (1), queue size (1)
Thread-0 plan to produce (5), actually produce (1), queue size (2)
Thread-0 plan to produce (5), actually produce (1), queue size (2)

# 有四个线程参与了这个过程,两个生产者,两个消费者

生产者消费者问题--BlockingQueue的更多相关文章

  1. 多线程-生产者消费者(BlockingQueue实现)

    三.采用BlockingQueue实现 BlockingQueue也是java.util.concurrent下的主要用来控制线程同步的工具. BlockingQueue有四个具体的实现类,根据不同需 ...

  2. Java多线程15:Queue、BlockingQueue以及利用BlockingQueue实现生产者/消费者模型

    Queue是什么 队列,是一种数据结构.除了优先级队列和LIFO队列外,队列都是以FIFO(先进先出)的方式对各个元素进行排序的.无论使用哪种排序方式,队列的头都是调用remove()或poll()移 ...

  3. 使用BlockingQueue的生产者消费者模式

    BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利.使用场景. 首先它是一个队列,而一个队 ...

  4. 生产者-消费者模型的3种Java实现:synchronized,signal/notifyAll及BlockingQueue

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3555111.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

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

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

  6. Queue和BlockingQueue的使用以及使用BlockingQueue实现生产者-消费者

    Java提供了两种新的容器类型:Queue和BlockingQueue. Queue用于保存一组等待处理的元素.它提供了几种实现,包括:ConcurrentLinkedQueue,这是一个先进先出的并 ...

  7. Java阻塞队列(BlockingQueue)实现 生产者/消费者 示例

    Java阻塞队列(BlockingQueue)实现 生产者/消费者 示例 本文由 TonySpark 翻译自 Javarevisited.转载请参见文章末尾的要求. Java.util.concurr ...

  8. 生产者-消费者中的缓冲区:BlockingQueue接口

    BlockingQueue接口使用场景相信大家对生产者-消费者模式不陌生,这个经典的多线程协作模式,最简单的描述就是生产者线程往内存缓冲区中提交任务,消费者线程从内存缓冲区里获取任务执行.在生产者-消 ...

  9. Java多线程(十):BlockingQueue实现生产者消费者模型

    BlockingQueue BlockingQueue.解决了多线程中,如何高效安全"传输"数据的问题.程序员无需关心什么时候阻塞线程,什么时候唤醒线程,该唤醒哪个线程. 方法介绍 ...

随机推荐

  1. 2019-2020 ICPC, Asia Jakarta Regional Contest A. Copying Homework

    Danang and Darto are classmates. They are given homework to create a permutation of N integers from  ...

  2. 软件测试第2周个人作业:WordCount编码测试

    一.Github地址 https://github.com/zhouyubei/WordCount 二.PSP表格 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning ...

  3. Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)

    Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...

  4. (转)window.XMLHttpRequest详解(AJAX工作原理)

    转自:http://l.xbest.blog.163.com/blog/static/8640444120100225516963/?fromdm&fromSearch&isFromS ...

  5. 【DSP开发】DSP通用并行端口uPP

      这是翻译TI官方文档<KeyStone Architecture Universal Parallel Port (uPP)>SPRUHG9有关通用并行端口uPP的内容(除寄存器部分) ...

  6. BTree B+Tree

    简介 B 树是为了磁盘或其它存储设备而设计的一种多叉平衡查找树.(相对于二叉,B树每个内结点有多个分支,即多叉)B树又可以写成B-树/B-Tree,并不是B“减”树,横杠为连接符,容易被误导首先我们介 ...

  7. [bzoj2288]【POJ Challenge】生日礼物_贪心_堆

    [POJ Challenge]生日礼物 题目大意:给定一个长度为$n$的序列,允许选择不超过$m$个连续的部分,求元素之和的最大值. 数据范围:$1\le n, m\le 10^5$. 题解: 显然的 ...

  8. Servlet中获取POST请求的参数

    在servlet.filter等中获取POST请求的参数 form表单形式提交post方式,可以直接从 request 的 getParameterMap 方法中获取到参数 JSON形式提交post方 ...

  9. C++变量的声明和定义

    1.变量的定义:变量的定义用于为变量分配存储控件,还可以为变量指定初始值.在一个程序中,变量有且仅有一个定义. 2.变量的声明:用于向程序表名变量的类型和名字.程序中变量可以声明多次,但只能定义一次. ...

  10. 虚拟机(Vmware)安装ubuntu18.04和配置调整(二)

    二.配置修改 1.修改语言环境(settings->Region & Language) 选中中文简体(Chinese(simplified)),点击Apply 中文简体语言安装完成后, ...