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 ...
随机推荐
- iscroll.js & flipsnap.js
两个js都可以用做手机的滑动框架iscroll.js功能更多flipsnap.js应该只能水平滑动. iscroll.js介绍http://iiunknown.gitbooks.io/iscroll- ...
- 使用Jquery解析Json
利用原生JSON对象,将对象转为字符串 [javascript] view plaincopy var jsObj = {}; jsObj.testArray = [1,2,3,4,5]; j ...
- PHP删除Solr文档
<?php $options = array ( 'hostname' => 'localhost', 'port' => '8080', 'path'=>'solr/help ...
- C#的SerialPort串口程序设计总结
简介:微软的VS提供了SerialPort控件,也就是串行端口资源. 当然也可以添加引用 using System.IO.Ports; 通过实例化SerialPort对象就可以使用其属性和方法了. S ...
- php 随机显示据今天30天内的任意一天
function randomDate() { //echo date( "Y-m-d H:m:s", $newtime); //echo date("Y-m-d H:m ...
- 「30天自制操作系统」 Stop & 「OS67 」 Start
废话 整个十月都没有再写一点什么, 其实没什么好写的, 把书里的东西码出来贴在博客里实在没什么意思, 况且书里已经写得够详细了. 这本书给我最深刻的感觉是, 作者通过简化一些细节, 一步一步地模拟整个 ...
- 不同浏览器使用Content-disposition时filename带空格的处理方式不同
最近在做项目中遇到一个问题,纠结了好久才找到原因.起因:通过MIME的扩展Content-disposition来实现在客户端保存附加文件(快捷方式).问题:在chrome和IE8+下一切都很和谐,浏 ...
- 7816的报文结构APDU
命令APDU 包括头和主体(这可以在上面的图中看到).头包括CLA,INS,P1 和P2 域.同T0 协议一样,CLA 和INS 说明了应用的分类和指令.P1 和P2 用来详细说明具体指令,并由每一条 ...
- POJ3687 Labeling Balls(拓扑)
题目链接. 题目大意: N个球,从1-N编号,质量不同,范围1-N,无重复.给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量.按编号输出每个球的标签.如果解不唯一,按编号小 ...
- fmt命令
简单的格式化文本 fmt [option] [file-list] fmt通过将所有非空白行的长度设置为几乎相同,来进行简单的文本格式化 参数 fmt从file-list中读取文件,并将其内容的格式化 ...