1700450725
1700450726
//最完整的构造函数
1700450727
1700450728
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long
1700450729
1700450730
keep Alive Time, Time Unitunit, Block in gQueue<Runnable>
1700450731
1700450732
workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler){
1700450733
1700450734
//检验输入条件
1700450735
1700450736
if(corePoolSize<0||maximumPoolSize<=0||
1700450737
1700450738
maximumPoolSize<corePoolSize||keepAliveTime<0)
1700450739
1700450740
throw new IllegalArgumentException();
1700450741
1700450742
//检验运行环境
1700450743
1700450744
if(workQueue==null||threadFactory==null||handler==null)
1700450745
1700450746
throw new NullPointerException();
1700450747
1700450748
this.corePoolSize=corePoolSize;
1700450749
1700450750
this.maximumPoolSize=maximumPoolSize;this.workQueue=workQueue;
1700450751
1700450752
this.keepAliveTime=unit.toNanos(keepAliveTime);this.threadFactory=threadFactory;
1700450753
1700450754
this.handler=handler;
1700450755
1700450756
}
1700450757
1700450758
}
1700450759
1700450760
这是ThreadPoolExecutor最完整的构造函数,其他的构造函数都是引用该构造函数实现的,我们逐步来解释这些参数的含义。
1700450761
1700450762
corePoolSize:最小线程数。
1700450763
1700450764
线程池启动后,在池中保持线程的最小数量。需要说明的是线程数量是逐步到达corePoolSize值的,例如corePoolSize被设置为10,而任务数量只有5,则线程池中最多会启动5个线程,而不是一次性地启动10个线程。
1700450765
1700450766
maximumPoolSize:最大线程数量。
1700450767
1700450768
这是池中能够容纳的最大线程数量,如果超出,则使用RejectedExecutionHandler拒绝策略处理。
1700450769
1700450770
keepAliveTime:线程最大生命期。
1700450771
1700450772
这里的生命期有两个约束条件:一是该参数针对的是超过corePoolSize数量的线程;二是处于非运行状态的线程。这么说吧,如果corePoolSize为10,maximumPoolSize为20,此时线程池中有15个线程在运行,一段时间后,其中有3个线程处于等待状态的时间超过了keepAliveTime指定的时间,则结束这3个线程,此时线程池中则还有12个线程正在运行。
1700450773
1700450774
unit:时间单位。
[
上一页 ]
[ :1.700450725e+09 ]
[
下一页 ]