BlockingQueue案例
来源:互联网 发布:mysql.sock 编辑:程序博客网 时间:2024/05/18 02:32
一、BlockingQueue的主要方法:
1、插入
1)add(obj):把obj添加到BlockingQueue中,如果BlockingQueue可以容纳,则返回true,否则,抛出异常
2)offer(obj):把obj添加到BlockingQueue中,如果BlockingQueue可以容纳,则返回true,否则,返回false
3)put(obj):把obj添加到BlockingQueue中,如果BlockingQueue已满,则调用该方法的线程被阻塞,直到有空间再继续添加。
4)offer(obj,timeout,unit):把obj添加到BlockingQueue中,如果BlockingQueue可以容纳,则返回true,否则,等待,在设置的timeout时间段内,BlockingQueue有空间了,添加成功,返回true,否则,返回false。
2、读取
1)take():取走(删除并返回)BlockingQueue队列首部的元素,如果BlockingQueue对队列为空,则一直等待,直到有元素可以取出。
2)poll(timeout,unit):取走(删除并返回)BlockingQueue队列首部的元素,如果BlockingQueue队列为空,则等待,在设置的timeout时间段内,BlockingQueue有元素了,则返回,否则,返回null。
3、删除
1)remove(obj):从队列中删除元素obj,如果存在多个obj元素,则一并删除,删除成功(队列改变了),返回true。
4、其他
1)int remainingCapacity():返回队列中剩余的容量,在队列插入或获取的时候,不要瞎搞,数据可能不准,不能保证数据的准确性。
2)boolean contains(obj):判断队列中是否包含obj元素,至少包含一个,则返回true。
3)int drainTo(collection):移除队列中可用的元素,并将它们添加到给定的集合collection中,返回成功转移的元素的个数。
4)int drainTo(collection,maxElements):与drainTo(collection)方法相似,该方法给定了最大移除元素的个数maxElements。
二、BlockingQueue的四种主要实现中的两种常见的实现
1、ArrayBlockingQueue:是一个由数组支持的有界阻塞队列,一创建大小就固定了,而且是不能够改变的BlockingQueue,其构造函数必须带一个int参数来指明其大小,其所含的对象是以FIFO(先进先出)的顺序排列的。
2、LinkedBlockingQueue:是一个大小不固定的BlockingQueue,若其构造函数中带一个规定大小的参数,生成的BlockingQueue有大小的限制,若不指定该参数,生成的BlockingQueue的大小由Integer.MAX_VALUE来决定。其所含的对象是以FIFO(先进先出)顺序排列的。
3、ArrayBlockingQueue与LinkedBlockingQueu的区别:由于两种队列所使用的数据结构不一样,因此,在实际情况中,LinkedBlockingQueue的数据吞吐量要大于ArrayBlockingQueue,但在线程数量很大时,其性能的可预见性ArrayBlockingQueue要大于LinkedBlockingQueue。
三、使用BlockingQueue实现消息队列
package com.chen.test;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;public class MessageQueue { public static void main(String[] args) { //创建队列 BlockingQueue<String> queue = new ArrayBlockingQueue<String>(5); //创建生产者 new Thread(new Producer(queue)).start(); //创建消费者 new Thread(new Consumer(queue)).start(); }}
Producer类
package com.chen.test;import java.util.concurrent.BlockingQueue;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class Producer implements Runnable{ private BlockingQueue<String> queue; public Producer(BlockingQueue<String> queue) { this.queue = queue; } @Override public void run() { //创建线程池 ExecutorService service = Executors.newFixedThreadPool(10); //创建10个生产者 for(int i=0;i<10;i++){ Future<String> future = service.submit(new Callable<String>() { @Override public String call() throws Exception { String threadName = Thread.currentThread().getName(); queue.put(threadName+":一条消息"); return threadName+"生产了一条消息"; } }); try { System.out.println(future.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } service.shutdown(); }}
Consumer类
package com.chen.test;import java.util.concurrent.BlockingQueue;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;public class Consumer implements Runnable { private BlockingQueue<String> queue; public Consumer(BlockingQueue<String> queue) { this.queue = queue; } @Override public void run() { //创建线程池 ExecutorService service = Executors.newFixedThreadPool(12); //创建12个生产者 for(int i=0;i<12;i++){ Future<String> future = service.submit(new Callable<String>() { @Override public String call() throws Exception { String element = queue.take(); return "消费者消费:"+element; } }); try { System.out.println(future.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } service.shutdown(); }}
四:运行结果:
pool-1-thread-1生产了一条消息
消费者消费:pool-1-thread-1:一条消息
pool-1-thread-2生产了一条消息
消费者消费:pool-1-thread-2:一条消息
pool-1-thread-3生产了一条消息
消费者消费:pool-1-thread-3:一条消息
pool-1-thread-4生产了一条消息
消费者消费:pool-1-thread-4:一条消息
pool-1-thread-5生产了一条消息
消费者消费:pool-1-thread-5:一条消息
pool-1-thread-6生产了一条消息
消费者消费:pool-1-thread-6:一条消息
pool-1-thread-7生产了一条消息
消费者消费:pool-1-thread-7:一条消息
pool-1-thread-8生产了一条消息
消费者消费:pool-1-thread-8:一条消息
pool-1-thread-9生产了一条消息
消费者消费:pool-1-thread-9:一条消息
pool-1-thread-10生产了一条消息
消费者消费:pool-1-thread-10:一条消息
- BlockingQueue案例
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- BlockingQueue
- 解决和说明> eth.getCompilers() Error: The method eth_getCompilers does not exist/is not available
- Linux服务器启动tomcat很慢解决方法
- 服务器架设
- 字符串调用split以点逗"."符号作为拆分符时返回的是空数组
- 从“适合”出发选择服务器空间!
- BlockingQueue案例
- 【Android笔记-6】strings.xml文件
- 设计师留住用户最有效的5种设计模式
- ROS学习相关链接【转】
- android 自定义View、SurfaceView对比
- (LightOJ
- discuz教程:修改帖子详情页“来自: XX手机客户端”的链接地址
- Camera CullingMask层处理
- Java反射 Filed.get(null)