码迷,mamicode.com
首页 > 编程语言 > 详细

ThreadPoolExecutor线程池

时间:2017-12-13 19:44:19      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:exce   tac   count()   string   线程池   import   ack   exception   public   

 1 package unit;
 2 
 3 import java.util.concurrent.ArrayBlockingQueue;
 4 import java.util.concurrent.ThreadPoolExecutor;
 5 import java.util.concurrent.TimeUnit;
 6 
 7 public class ThreadPoolExecutor线程池 {
 8     public static void main(String[] args) {
 9         ThreadPoolExecutor executorPool = new ThreadPoolExecutor(5, 15, 2000, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(5));
10         for(int i=0; i<15; i++) {
11             executorPool.execute(new MyTask(i));
12             System.out.println("线程池中线程数目:"+executorPool.getPoolSize()+",队列中等待执行的任务数目:"+
13             executorPool.getQueue().size()+",已执行过的任务数目:"+executorPool.getCompletedTaskCount());
14         }
15         System.out.println("=========================");
16         executorPool.shutdown();
17         executorPool.setKeepAliveTime(2, TimeUnit.MINUTES);
18         
19     }
20 }
21 
22 class MyTask implements Runnable{
23     int theTask = 0;
24     public MyTask() {
25         
26     }
27     public MyTask(int i) {
28         theTask = i;
29         run();
30     }
31     @Override
32     public void run() {
33         System.out.println("当前正在执行第: "+ theTask + " 个线程");
34         /* try {
35             Thread.sleep(1000);
36         } catch (InterruptedException e) {
37             e.printStackTrace();
38         }*/
39         System.out.println("task "+theTask+"执行完毕");
40     }
41 }

 

ThreadPoolExecutor线程池

标签:exce   tac   count()   string   线程池   import   ack   exception   public   

原文地址:http://www.cnblogs.com/redhat0019/p/8034191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!