队列+多线程实例

原文章链接:https://blog.csdn.net/cai_chinasoft/article/details/51566632 

第一步:创建一个无边界自动回收的线程池,在此用 JDK提供的ExecutorService类

此线程池。如果线程池的大小超过了处理任务所需要的线程,那么就会回收部分空闲(60秒不执行任务)的线程,当任务数增加时,此线程池又可以智能的添加新线程来处理任务。此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说JVM)能够创建的最大线程大小。


[java]  view plain  copy
  1. package com.thread.test;  
  2.   
  3. import java.util.concurrent.ExecutorService;  
  4. import java.util.concurrent.Executors;  
  5.   
  6. public class ThreadPool {  
  7.     private static ExecutorService threadPool = null;  
  8.     public static ExecutorService getThreadPool(){  
  9.         if(threadPool==null){  
  10.             threadPool = Executors.newCachedThreadPool();  
  11.         }  
  12.         return  threadPool;  
  13.     }  
  14.   
  15. }  

第二步:使用单例模式创建一个无界队列,并提供入队的方法

无界队列。使用无界队列(例如,不具有预定义容量的 LinkedBlockingQueue)将导致在所有corePoolSize 线程都忙时新任务在队列中等待。这样,创建的线程就不会超过 corePoolSize。(因此,maximumPoolSize的值也就无效了。)当每个任务完全独立于其他任务,即任务执行互不影响时,适合于使用无界队列;例如,在 Web页服务器中。这种排队可用于处理瞬态突发请求,当命令以超过队列所能处理的平均数连续到达时,此策略允许无界线程具有增长的可能性。

[java]  view plain  copy
  1. package com.thread.test;  
  2.   
  3. import java.util.concurrent.LinkedBlockingQueue;  
  4.   
  5. public class TaskQueue {  
  6.     private static  LinkedBlockingQueue queues = null;  
  7.       
  8.     public static LinkedBlockingQueue getTaskQueue(){  
  9.         if(queues==null){  
  10.             queues =  new LinkedBlockingQueue();  
  11.             System.out.println("初始化 队列");  
  12.         }  
  13.         return queues;  
  14.     }  
  15.       
  16.     public static void add(Object obj){  
  17.         if(queues==null)  
  18.             queues =  getTaskQueue();  
  19.         queues.offer(obj);  
  20.         System.out.println("-------------------------------");  
  21.         System.out.println("入队:"+obj);  
  22.     }  
  23. }  

第三步:提供一个入队的线程,实际使用中的生产者

[java]  view plain  copy
  1. package com.thread.test;  
  2.   
  3. public class Produce implements Runnable {  
  4.     private static volatile int i=0;  
  5.     private static volatile boolean isRunning=true;  
  6.   
  7.     public void run() {  
  8.         while(isRunning){  
  9.             TaskQueue.add(Integer.valueOf(i+""));  
  10.             Produce.i++;  
  11.             try {  
  12.                 Thread.sleep(1*1000);  
  13.             } catch (InterruptedException e) {  
  14.                 e.printStackTrace();  
  15.             }  
  16.         }  
  17.           
  18.     }  
  19.   
  20. }  

第四步:提供一个出队的线程,实际使用中的消费者

[java]  view plain  copy
  1. package com.thread.test;  
  2.   
  3. public class Consumer implements Runnable {  
  4.     private static Consumer consumer;  
  5.       
  6.     public static volatile boolean isRunning=true;  
  7.     public void run() {  
  8.         while(Thread.currentThread().isInterrupted()==false && isRunning)    
  9.         {    
  10.             try {  
  11.                 System.out.println("出队"+TaskQueue.getTaskQueue().take());  
  12.                 Thread.sleep(1*1000);    
  13.             } catch (InterruptedException e) {  
  14.                 e.printStackTrace();  
  15.             }  
  16.         }  
  17.           
  18.     }  
  19.     public static Consumer getInstance(){  
  20.         if(consumer==null){  
  21.             consumer = new Consumer();  
  22.             System.out.println("初始化消费线程");  
  23.         }  
  24.         return consumer;  
  25.     }  
  26.   
  27. }  

第五步:启动生产消费策略

[java]  view plain  copy
  1. package com.thread.test;  
  2.   
  3. import java.util.concurrent.ExecutorService;  
  4. import java.util.concurrent.LinkedBlockingQueue;  
  5.   
  6. public class Test {  
  7.       
  8.     public static void main(String[] args) {  
  9.         ExecutorService threadPool = ThreadPool.getThreadPool();  
  10.         Produce consumer2 = new Produce();  
  11.         threadPool.execute(consumer2);  
  12.         Consumer consumer=Consumer.getInstance();  
  13.         threadPool.execute(consumer);  
  14.     }  
  15.   
  16. }  
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值