ThreadPoolExecutor整体图
- worker是带着任务的thread,最终执行者为worker这个thread。
- worker会被放入workers map集合中使用。
ThreadPoolExecutor工作流程图
ThreadPoolExecutor源码
ThreadPoolExecutor主要参数
线程池状态
//高三位bit位表示状态
//111为running
private static final int RUNNING = -1 << COUNT_BITS;
//000为 shutdown
private static final int SHUTDOWN = 0 << COUNT_BITS;
//001为stop
private static final int STOP = 1 << COUNT_BITS;
//010为tidying
private static final int TIDYING = 2 << COUNT_BITS;
//011为terminated
private static final int TERMINATED = 3 << COUNT_BITS;
- running : 111 :接受新任务(队列);
- shutdown:000 : 不接受任务,执行队列任务;
- stop : 001 : 不执行新任务,不执行对列任务,打断;
- tidying :010 : 任务之行结束,准备终止
- terminated :011 : 终止
execute方法
public void execute(Runnable command) {
//没任务直接抛异常************************************************//
if (command == null)
throw new NullPointerException();
//正在运行的线程数量new AtomicInteger(ctlOf(RUNNING,0));***********//
int c = ctl.get();
//判断核心线程是否满************************************************//
if (workerCountOf(c) < corePoolSize) {
//核心线程未满加入核心线程worker**********************************//
if (addWorker(command, true))
return;
c = ctl.get();
}
//核心线程满 任务加入队列**********************************//
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
//队列满 任务加入非核心worker**********************************//
else if (!addWorker(command, false))
//任务加入非核心worker失败就拒绝策略**********************************//
reject(command);
}
addWorker()方法
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
//从工厂创建线程,并把传进来的任务赋予worker
this.thread = getThreadFactory().newThread(this);
}
woker的run方法 调用runWorker()方法
public void run() {
//worker的run方法实际调用的是外部的runWorker方法
runWorker(this);
}
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
//取任务方法*********************************************************//
while (task != null || (task = getTask()) != null) {
w.lock();
// If pool is stopping, ensure thread is interrupted;
// if not, ensure thread is not interrupted. This
// requires a recheck in second case to deal with
// shutdownNow race while clearing interrupt
if ((runStateAtLeast(ctl.get(), STOP) ||
(Thread.interrupted() &&
runStateAtLeast(ctl.get(), STOP))) &&
!wt.isInterrupted())
wt.interrupt();
try {
beforeExecute(wt, task);
Throwable thrown = null;
try {
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
- while死循环不断去getTask,这也是为什么创建的线程就会重复利用的原因。
getTask()方法 同时会判断超时淘汰
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
// 判断超时淘汰*************************************************************//
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
ThreadPoolExecutor拒绝策略
AbortPolicy终止策略:抛异常
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());
}
}
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) {
}
}
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);
}
}
}
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();
}
}
}
参数设置(参考)
CPU密集型:CPU核数+1
IO密集型:2CPU核数+1
最佳线程数 = CPU 核数 * ( 1+( I/O耗时 / CPU耗时))