BlockingQueue接口
public class BlockingQueueTest {
//最大容量为5的数组堵塞队列private static ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(5, true);//private static LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(5);private static CountDownLatch producerLatch; //生产者倒计时计数器private static CountDownLatch consumerLatch;//消费者倒计时计数器public static void main(String[] args) {producerLatch = new CountDownLatch(10); //state值为10consumerLatch = new CountDownLatch(10); //state值为10new Thread(new ProducerTask()).start();new Thread(new ConsumerTask()).start();try {System.out.println("producer waiting...");producerLatch.await(); //进入等待状态,直到state值为0,再继续往下执行System.out.println("producer end");System.out.println("consumer waiting...");consumerLatch.await(); //进入等待状态,直到state值为0,再继续往下执行System.out.println("consumer end");} catch (InterruptedException e) {e.printStackTrace();}System.out.println("end");}//******************************************************************************************//生产者private static class ProducerTask implements Runnable {private Random rnd = new Random();@Overridepublic void run() {try {while (true) {queue.put(rnd.nextInt(100)); //如果queue容量已满,则当前线程会堵塞,直到有空间再继续//offer方法为非堵塞的//queue.offer(rnd.nextInt(100), 1, TimeUnit.SECONDS); //等待1秒后还不能加入队列则返回失败,放弃加入//queue.offer(rnd.nextInt(100));producerLatch.countDown(); //state值减1//TimeUnit.SECONDS.sleep(2); //线程休眠2秒}} catch (Exception ex) {ex.printStackTrace();}}}//消费者private static class ConsumerTask implements Runnable {@Overridepublic void run() {try {while (true) {Integer value = queue.take(); //如果queue为空,则当前线程会堵塞,直到有新数据加入//poll方法为非堵塞的//Integer value = queue.poll(1, TimeUnit.SECONDS); //等待1秒后还没有数据可取则返回失败,放弃获取//Integer value = queue.poll();System.out.println("value = " + value);consumerLatch.countDown(); //state值减1TimeUnit.SECONDS.sleep(2); //线程休眠2秒}} catch (Exception ex) {ex.printStackTrace();}}}}
BlockingQueue接口的更多相关文章
- 生产者-消费者中的缓冲区:BlockingQueue接口
BlockingQueue接口使用场景相信大家对生产者-消费者模式不陌生,这个经典的多线程协作模式,最简单的描述就是生产者线程往内存缓冲区中提交任务,消费者线程从内存缓冲区里获取任务执行.在生产者-消 ...
- 并发队列 ConcurrentLinkedQueue 及 BlockingQueue 接口实现的四种队列
队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 在队列这 ...
- Java.util.concurrent包学习(一) BlockingQueue接口
JDK1.7 BlockingQueue<E>接口 (extends Queue<E>) 所有父接口:Collection<E>,Iterable<E> ...
- Java:concurrent包下面的Collection接口框架图( CopyOnWriteArraySet, CopyOnWriteArrayList,ConcurrentLinkedQueue,BlockingQueue)
Java集合大致可分为Set.List和Map三种体系,其中Set代表无序.不可重复的集合:List代表有序.重复的集合:而Map则代表具有映射关系的集合.Java 5之后,增加了Queue体系集合, ...
- ArrayBlockingQueue,BlockingQueue分析
BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,让容量满时往BlockingQueue中添加数据时会造成阻塞,当容量为空时取元素操作会 ...
- 并发队列之:BlockingQueue和ConcurrentLinkedQueue
一.并行和并发区别: 并行:是指两者同时执行一件事.比如赛跑,两个人都在不停的往前跑: 并发:是指资源有限的情况下,两者交替轮流使用资源.比如一段路(单核CPU资源)同时只能过一个人,A走一段后,让给 ...
- 项目积累——Blockingqueue,ConcurrentLinkedQueue,Executors
背景 通过做以下一个小的接口系统gate,了解一下mina和java并发包里的东西.A系统为javaweb项目,B为C语言项目,gate是本篇须要完毕的系统. 需求 1. A为集群系统,并发较高,会批 ...
- java.util.concurrent BlockingQueue
BlockingQueue 它实现了Queue接口.它是A BlockingQueue with one thread putting into it, and another thread taki ...
- java并发包——阻塞队列BlockingQueue及源码分析
一.摘要 BlockingQueue通常用于一个线程在生产对象,而另外一个线程在消费这些对象的场景,例如在线程池中,当运行的线程数目大于核心的线程数目时候,经常就会把新来的线程对象放到Blocking ...
随机推荐
- JVM笔记-temp
jvm源码分析之堆外内存完全解读 http://lovestblog.cn/blog/2015/05/12/direct-buffer/
- Erget 显示对象
核心显示类: 类 描述 DisplayObject 显示对象基类,所有显示对象均继承自此类 Bitmap 位图,用来显示图片 Shape 用来显示矢量图,可以使用其中的方法绘制矢量图形 TextFie ...
- Scut 进阶:Schema 自动检测
Scut 在启动时有一个自动根据代码中数据类型检查数据库字段的功能,要如何使用呢? 脚本引擎动态加载 ModelAssembly: ScriptEngine.cs - InitScriptRuntim ...
- nutch fetcher.server.delay
1 配置因素 <property> <name>fetcher.server.delay</name> <value>0.0</value&g ...
- SCLButton
import UIKit let kSuccessTitle = "Congratulations" let kErrorTitle = "Connection erro ...
- 7.3.2 Using Backups for Recovery 使用备份用于恢复
7.3.2 Using Backups for Recovery 使用备份用于恢复 现在,假设我们有一个灾难性的crash 在星期三上午8点. 需要恢复从备份恢复,为了恢复,我们首选需要恢复最近的全备 ...
- 转自http://blog.sina.com.cn/daylive——C++ STL map
Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作! 1.map最基本的构造函数: map<string ...
- centos 64bit 安装与升级 chrome的方法
vim /etc/yum.repos.d/CentOS-Base.repo 根据你的系统增加一个节点 [google64] name=Google - x86_64 baseurl=http://dl ...
- MapReduce的reduce函数里的key用的是同一个引用
最近写MapReduce程序,出现了这么一个问题,程序代码如下: package demo; import java.io.IOException; import java.util.HashMap; ...
- AOJ 0121 广度优先搜索
题意:7数码问题.在2×4的棋盘上,摆有7个棋子,每个棋子上标有1至7的某一数字,不同棋子上标的数字不相同.棋盘上还有一个空格(用0表示),与空格相邻(上下左右)的棋子可以移到空格中,该棋子原先位置成 ...