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 ...
随机推荐
- ERROR:the server has either erred or is incapable of performing the requested operation
openstack中,有时会经常出现这种错误,原因无二,一是安全组没有设置正确,二是openstack中网络配置会有些问题或者是相关的服务没有启动. 解决方法:1.安全组问题在nova.conf和ne ...
- 使用mysql作为hive的元数据库
1.hive下载安装 2.下载mysql安装 3.以root用户进入mysql命令行:mysql -uroot -p(提示输入密码) 4.创建hive的元数据库:create databa ...
- 正确设置网站title、keywords、description(转载)
本文转载自蚂蚁HR(www.mayihr.com) 优化技巧是老师在课堂上教不了你的,而自己也不可能在练习中领悟,最便捷的方法就是听取别人的经验,所以转载一下 1.title(网站标题) title, ...
- C# Trim方法去除字符串两端的指定字符
var str= ",2,3,4,6,7,"; var str2 = str.Trim(new char[] { ',' }); //去除字符串str两端的','字符. //则st ...
- Highcharts中文网
官网地址: http://www.hcharts.cn/ http://www.hcharts.cn/p/highchartTable.php 名词解释 英文名 中文名 描述 更多 lang 语言 ...
- c#中方法out参数的使用
一个很普通的例题,求出一个整型数组的最小值.最大值.总和.平均值,利用调用函数的方法来ut参数实现 using System; using System.Collections.Generic; us ...
- Django filter中用contains 在mysql中的问题
用PYTHON ,DJANGO 做站,在通常的情况下,需要用到 orM 的查询方法,比如object.filter(tag__contains='keywords').... 在这种情况下,如果你跟踪 ...
- 关于自定义Adapter实现ListView的使用
以下为使用BaseAdapter作扩展,自定义Adapter来使用ListView控件: 需要注意以下的几点: 1.自定义Adapter时,需要特别注意Adapter类中getView()方法覆盖,注 ...
- UVA - 11346 Probability (概率)
Description Probability Time Limit: 1 sec Memory Limit: 16MB Consider rectangular coordinate system ...
- Foreign Exchange(交换生换位置)
Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enth ...