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 }