标签:单位 机制 work timeout 任务 设置 val 时长 通过
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
}
corePoolSize:核心线程数,会在线程池中一直存活,即时处于闲置状态;但如果将ThreadPoolExecutor的allowCoreThreadTimeOut设置为true,那么闲下来的核心线程就有超时策略,由keepAliveTime决定,时长到后,核心线程会终止运行
如果将ThreadPoolExecutor的allowCoreThreadTimeOut设置为true,那么闲下来的核心线程就有超时策略,由keepAliveTime决定,
newFixedThreadPool;线程数量固定的线程池,没有超时,不会被关闭核心线程(只有核心线程),除非线程池被关闭了;任务队列没有限制

newCachedThreadPool():没有核心线程只有非核心线程,没有任务队列,有60s超时机制,查过60s闲置线程就会被回收,适合执行大量的耗时比较少的任务。所有线程超时终止,是不占用任何资源的

public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
newScheduledThreadPool:固定核心线程,非核心线程Integer.MAX_VALUE,执行定时任务和具有固定重复周期的重复任务
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE,
DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
new DelayedWorkQueue());
}
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
long delay,
TimeUnit unit) {
标签:单位 机制 work timeout 任务 设置 val 时长 通过
原文地址:https://www.cnblogs.com/endian11/p/10449972.html