标签:str max 四种 tco 队列 长度 keepaliv nts scheduled
java.uitl.concurrent.ThreadPoolExecutor类是线程池中最核心的一个类。
ThreadPoolExecutor extends AbstractExecutorService
构造方法属性:corePoolSize、maximumPoolSize、keepAliveTime、unit、workQueue、threadFactory、handler
1.Executor是一个顶层接口,在它里面只声明了一个方法execute(Runnable)返回值为void,参数为Runnable类型,就是用来执行传进去的任务的;
2.然后ExecutorService接口继承了Executor接口,并声明了一些方法:submit、invokeAll、invokeAny以及shutDown等;
3.抽象类AbstractExecutorService实现了ExecutorService接口,基本实现了ExecutorService中声明的所有方法;
4.然后ThreadPoolExecutor继承了类AbstractExecutorService。
在ThreadPoolExecutor类中有几个非常重要的方法:
execute():向线程池提交一个任务,交由线程池去执行。
submit():也是用来向线程池提交任务的,能够返回任务执行的结果(利用Future)。
shutdown()和shutdownNow()是用来关闭线程池的。
执行过程:
1 public class Test { 2 public static void main(String[] args) { 3 ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS, 4 new ArrayBlockingQueue<Runnable>(5)); 5 6 for(int i=0;i<15;i++){ 7 MyTask myTask = new MyTask(i); 8 executor.execute(myTask); 9 System.out.println("线程池中线程数目:"+executor.getPoolSize()+",队列中等待执行的任务数目:"+ 10 executor.getQueue().size()+",已执行玩别的任务数目:"+executor.getCompletedTaskCount()); 11 } 12 executor.shutdown(); 13 } 14 } 15 16 17 class MyTask implements Runnable { 18 private int taskNum; 19 20 public MyTask(int num) { 21 this.taskNum = num; 22 } 23 24 @Override 25 public void run() { 26 System.out.println("正在执行task "+taskNum); 27 try { 28 Thread.currentThread().sleep(4000); 29 } catch (InterruptedException e) { 30 e.printStackTrace(); 31 } 32 System.out.println("task "+taskNum+"执行完毕"); 33 } 34 }
Java通过Executors提供四种线程池,分别为:
public class ThreadPoolExecutorTest { public static void main(String[] args ) { ExecutorService cacheThreadPool =Executors.newCachedThreadPool(); for(int i =1;i<=5;i++){ final int index=i ; try{ Thread.sleep(1000); }catch(InterruptedException e ) { e.printStackTrace(); } cacheThreadPool.execute(new Runnable(){ @Override public void run() { System.out.println("第" +index +"个线程" +Thread.currentThread().getName()); } }); } } }
标签:str max 四种 tco 队列 长度 keepaliv nts scheduled
原文地址:https://www.cnblogs.com/m4tech/p/9958912.html