线程池构造函数
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
核心线程数是不管有没有任务都会存在的,当有任务的时候,先塞满核心线程,然后,去塞满队列,然后,在创建新的线程,塞满最大线程数,然后,执行决绝策略,策略有四种:1.直接抛出异常;2.在调用的线程里面执行,如果,调用线程关闭,就丢弃;3.丢弃队列头未执行的任务;4.直接丢弃,什么都不做
参数详解
- int corePoolSize
- int maximumPoolSize
- long keepAliveTime
- TimeUnit unit
- BlockingQueue workQueue
- ThreadFactory threadFactory
- RejectedExecutionHandler handler
corePoolSize
corePoolSize 线程池中一直存活的线程数量,哪怕他们是空闲的,
可以设置 allowCoreThreadTimeOut 来管理他们的存活时间
maximumPoolSize
maximumPoolSize 线程池中最大允许存活的线程数量,
不能小于 corePoolSize
keepAliveTime
keepAliveTime 当线程池中的线程数量大于corePoolSize的时候,空闲的线程等待新任务的时间
unit
unit keepAliveTime 的时间单位
workQueue
workQueue 用于保存等待执行的任务的阻塞队列
- LinkedBlockingQueue 线程安全,先进先出,使用生产者和消费者模式,可以设置长度,默认长度Integer.MAX_VALUE.
- SynchronousQueue 后进先出,没有容量
- DelayedWorkQueue
threadFactory
默认的线程工厂类,设置线程的group和名称前缀
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
RejectedExecutionHandler handler
拒绝策略
- AbortPolicy 直接抛出异常 RejectedExecutionException
public static class AbortPolicy implements RejectedExecutionHandler {
/**
* Creates an {@code AbortPolicy}.
*/
public AbortPolicy() { }
/**
* Always throws RejectedExecutionException.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
* @throws RejectedExecutionException always.
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
- CallerRunsPolicy 被拒绝的任务,它直接在调用线程中执行,如果,线程关闭,则丢弃
public static class CallerRunsPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code CallerRunsPolicy}.
*/
public CallerRunsPolicy() { }
/**
* Executes task r in the caller's thread, unless the executor
* has been shut down, in which case the task is discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
- DiscardOldestPolicy 丢弃队列头的未执行的任务,执行新的任务
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardOldestPolicy} for the given executor.
*/
public DiscardOldestPolicy() { }
/**
* Obtains and ignores the next task that the executor
* would otherwise execute, if one is immediately available,
* and then retries execution of task r, unless the executor
* is shut down, in which case task r is instead discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}
- DiscardPolicy 直接丢弃任务,什么都不做
public static class DiscardPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardPolicy}.
*/
public DiscardPolicy() { }
/**
* Does nothing, which has the effect of discarding task r.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}