java-并发集合-阻塞队列 LinkedBlockingQueue 演示
2017-06-28 15:16
886 查看
java-并发集合-阻塞队列 LinkedBlockingQueue 演示
package me.grass.demo.concuronte; import java.util.Date; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; /** * 阻塞队列 java.util.concurrent.LinkedBlockingQueue 使用 * 阻塞队列提供两组入队、出队方法,分别为: * 无阻塞:add();poll(); * 阻塞:put();take(); */ public class LinkedBlockingQueueDemo { /** * 生产慢,消费快,导致队里阻塞 * @param args * @throws InterruptedException * @author xxj */ public static void main(String[] args) throws InterruptedException { /*测试逻辑: * 1.阻塞队列固定大小50,100个线程写入将产生写入阻塞 * 2.消费者比生产者快,消费时会产生读取阻塞 * */ Date before=new Date(); int pTotalThread =100; //最大线程数(生产者) int pActivities=5; //最大线程数(生产者) int cTotalThread =100; //活动线程数(消费者) int cActivities=5; //活动线程数(消费者) _lCountDownLatch = new CountDownLatch(pTotalThread+cTotalThread); startProducer(pActivities,pTotalThread); startConsumer(cActivities,cTotalThread); _lCountDownLatch.await();//等待所有线程完成 Date after = new Date(); System.out.println("队列为空:"+_blockingQueue.isEmpty()); System.out.println("耗时:"+((after.getTime()-before.getTime())/1000)); System.out.println("同步队列:"+_lCountDownLatch.getCount()); } static java.util.concurrent.CountDownLatch _lCountDownLatch; /** * 阻塞队列,队列固定大小50,100个线程写入将产生写入阻塞 */ static java.util.concurrent.LinkedBlockingQueue<Integer> _blockingQueue = new java.util.concurrent.LinkedBlockingQueue<Integer>(50); static void startProducer(int active,int totalThread) throws InterruptedException{ java.util.concurrent.ExecutorService pool = Executors.newFixedThreadPool(active); int size =1024*1024*10;//产生 3 M 数据 Thread thread ; for(int i=0;i<totalThread;i++){ thread = new Thread(new producer(i,size)); pool.execute(thread); } } static void startConsumer(int active,int totalThread) throws InterruptedException{ java.util.concurrent.ExecutorService pool = Executors.newFixedThreadPool(active); Thread thread ; //启动x个消费者 for(int i=0;i<totalThread;i++){ thread = new Thread(new consumer()); pool.execute(thread); } } /** * 生产者 * @author xxj * */ static class producer implements Runnable{ public producer(int key,int size){ _size=size; _key=key; } int _key; int _size; public void run() { try { //阻塞队列,使用 put 加入队列,如果队列满了将阻塞 LinkedBlockingQueueDemo._blockingQueue.put(_key);//生产 System.out.println("已创建:"+_key); Thread.sleep(1000);//让生产慢点儿 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } LinkedBlockingQueueDemo._lCountDownLatch.countDown();//线程同步递减 } } /** * 消费者 * @author xxj * */ static class consumer implements Runnable{ public consumer(){ } public void run() { //循环消费,直到队列内容为空 try { //阻塞队列使用 take() 产生阻塞 ,并发队列使用 poll() Integer nInteger = LinkedBlockingQueueDemo._blockingQueue.take();//消费 System.err.println("消费:"+nInteger); Thread.sleep(100);//每次消费等一会儿 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } LinkedBlockingQueueDemo._lCountDownLatch.countDown();//线程同步递减 } } }
相关文章推荐
- JAVA并发之阻塞队列LinkedBlockingQueue与ArrayBlockingQueue
- Java 并发 --- 阻塞队列之LinkedBlockingQueue源码分析
- java-并发集合-并发队列 ConcurrentLinkedQueue 演示
- 深入剖析java并发之阻塞队列LinkedBlockingQueue与ArrayBlockingQueue
- Java并发之BlockingQueue 阻塞队列(ArrayBlockingQueue、LinkedBlockingQueue、DelayQueue、PriorityBlockingQueue、SynchronousQueue)
- Java并发包:阻塞队列(BlockingQueue)
- Java并发容器之非阻塞队列ConcurrentLinkedQueue
- 并发队列ConcurrentLinkedQueue和阻塞栈LinkedBlockingQueue用法和阻塞队列ArrayBlockingQueue
- Java多线程 阻塞队列和并发集合
- Java多线程 阻塞队列和并发集合
- Java多线程 阻塞队列和并发集合
- java并发库中的阻塞队列--BlockingQueue
- 关于java中阻塞队列(LinkedBlockingQueue)中任务执行失败后的重试机制几种方案
- Java并发包:阻塞队列(BlockingQueue)
- Java多线程 阻塞队列和并发集合
- Java常用的2种阻塞队列ArrayBlockingQueue和LinkedBlockingQueue
- Java多线程与并发库高级应用之阻塞队列BlockingQueue
- [Java 基础] 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
- (13)多线程与并发库之java5阻塞队列(BlockingQueue)的应用----子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程循环100次,如此循环50次
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法 在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列(先进先出)。