Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得

http://heipark.iteye.com/blog/1393847


newFixedThreadPool内部有个任务队列,假设线程池里有3个线程,提交了5个任务,那么后两个任务就放在任务队列了,即使前3个任务sleep或者堵塞了,也不会执行后两个任务,除非前三个任务有执行完的

newFixedThreadPool使用范例:

Java代码 收藏代码
  1. importjava.io.IOException;
  2. importjava.util.concurrent.ExecutorService;
  3. importjava.util.concurrent.Executors;
  4. publicclassTest{
  5. publicstaticvoidmain(String[]args)throwsIOException,InterruptedException{
  6. ExecutorServiceservice=Executors.newFixedThreadPool(2);
  7. for(inti=0;i<6;i++){
  8. finalintindex=i;
  9. System.out.println("task:"+(i+1));
  10. Runnablerun=newRunnable(){
  11. @Override
  12. publicvoidrun(){
  13. System.out.println("threadstart"+index);
  14. try{
  15. Thread.sleep(Long.MAX_VALUE);
  16. }catch(InterruptedExceptione){
  17. e.printStackTrace();
  18. }
  19. System.out.println("threadend"+index);
  20. }
  21. };
  22. service.execute(run);
  23. }
  24. }
  25. }

输出:
task: 1
task: 2
thread start0
task: 3
task: 4
task: 5
task: 6
task: 7
thread start1
task: 8
task: 9
task: 10
task: 11
task: 12
task: 13
task: 14
task: 15

从实例可以看到for循环并没有被固定的线程池阻塞住,也就是说所有的线程task都被提交到了ExecutorService中,查看Executors.newFixedThreadPool()如下:

public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}

可以看到task被提交都了LinkedBlockingQueue中。这里有个问题,如果任务列表很大,一定会把内存撑爆,如何解决?看下面:

Java代码 收藏代码
  1. importjava.io.IOException;
  2. importjava.util.concurrent.ArrayBlockingQueue;
  3. importjava.util.concurrent.BlockingQueue;
  4. importjava.util.concurrent.ThreadPoolExecutor;
  5. importjava.util.concurrent.TimeUnit;
  6. publicclassTest{
  7. publicstaticvoidmain(String[]args)throwsIOException,InterruptedException{
  8. BlockingQueue<Runnable>queue=newArrayBlockingQueue<Runnable>(3);
  9. ThreadPoolExecutorexecutor=newThreadPoolExecutor(3,3,1,TimeUnit.HOURS,queue,newThreadPoolExecutor.CallerRunsPolicy());
  10. for(inti=0;i<10;i++){
  11. finalintindex=i;
  12. System.out.println("task:"+(index+1));
  13. Runnablerun=newRunnable(){
  14. @Override
  15. publicvoidrun(){
  16. System.out.println("threadstart"+(index+1));
  17. try{
  18. Thread.sleep(Long.MAX_VALUE);
  19. }catch(InterruptedExceptione){
  20. e.printStackTrace();
  21. }
  22. System.out.println("threadend"+(index+1));
  23. }
  24. };
  25. executor.execute(run);
  26. }
  27. }
  28. }

输出:
task: 1
task: 2
thread start1
task: 3
task: 4
task: 5
task: 6
task: 7
thread start2
thread start7
thread start6

线程池最大值为4(??这里我不明白为什么是设置值+1,即3+1,而不是3),准备执行的任务队列为3。可以看到for循环先处理4个task,然后把3个放到队列。这样就实现了自动阻塞队列的效果。记得要使用ArrayBlockingQueue这个队列,然后设置容量就OK了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值