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 ...
随机推荐
- 初涉JavaScript模式系列 阶段总结及规划
总结 不知不觉写初涉JavaScript模式系列已经半个月了,没想到把一个个小点进行放大,竟然可以发现这么多东西. 期间生怕对JS的理解不到位而误导各位,读了很多书(个人感觉JS是最难的oo语言),也 ...
- IIS 7.5 部署ASP.Net MVC 网站
請務必註冊 ASP.NET 4.0:若是 32 位元則是 %WINDIR%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -ir 1.首先确定已经安 ...
- 35 Search Insert Position(找到数的位置Medium)
题目意思:在递增数组中找到目标数的位置,如果目标数不在数组中,返回其应该在的位置. 思路:折半查找,和相邻数比较,注意边界 class Solution { public: int searchIns ...
- C# Word
C# 操作word文档 1.c#操作word 在指定书签插入文字或者图片 1using Word = Microsoft.Office.Interop.Word; 2 3object Nothing ...
- grails通过findBy或findBy查找的结果集进行排序
原文:http://grails.org/doc/2.3.x/ref/Domain%20Classes/list.html list Purpose Lists instances of the do ...
- CSS 去除浏览器默认 轮廓外框
在默认情况下,点击 a 标签,input,或者添加click事件的时候,浏览器留下一个轮廓外框(chrome之下为蓝色)~ 然而这些默认的轮廓外框,有时候很影响美观,并不是我们想保留的. 我们应如何消 ...
- python+flask+mongodb+whoosh实现自己的搜索引擎(一):目录
python+flask+jieba+mongodb+whoosh实现自己的搜索引擎 一.目录 二.基于python的爬虫 三.网页去燥,URL去重 四.基于mongodb的数据存储 五.基于whoo ...
- 浅谈MIPS地址对齐问题
1.什么叫地址对齐? RISC 下使用访存指令读取或写入数据单元时,目标地址必须是所访问之数据单元字节数的整数倍,这个叫做地址对齐. 2.计算机主要的架构分哪两类?及其地址对齐在两者的区别? 计算机主 ...
- Serilog with Autofac
http://serilog.net/ ---不错的日志工具 1.直接绑定 builder.Register<ILogger>((c, p) => { return new Log ...
- 前端面试题第二波,要offer的看过来~
快来测试测试自己掌握能力吧! 1. class.forname的作用?为什么要用? 1).获取Class对象的方式:类名.class.对象.getClass().Class.forName(" ...