问题:
线程池是如何实现线程复用,如何并行执行多个任务的。
简单:
一般都是介绍,核心线程和最大线程数量,介绍创建线程的规则。缺少了,如何实现复用的。
本文以这个为出发点,简单分析线程池的复用。其实就是简单的几行源码分析,和线程池组件分析。
线程池的组件
-
N个线程(core,Max)
可以执行任务的若干个容器
-
阻塞队列 BlockingQueue
存放待执行任务
线程创建规则core/max
略
线程复用
即,如何将放入线程中的诸多任务,在N个线程中执行的。
-
ThreadPoolExecutor.execute()
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
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);
}
else if (!addWorker(command, false))
reject(command);
}
分析:可以看出:ThreadPoolExecutor.execute()的功能就是:
1、将任务添加至阻塞队列workQueue,workQueue.offer(command)
2、根据core和maxPool,选择是否创建Worker,addWorker()
因此,线程复用的实现应该在worker中,打开addWorker()方法观察
-
addWorker
private boolean addWorker(Runnable firstTask, boolean core) {
//创建worker
retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
for (;;) {
int wc = workerCountOf(c);
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
if (compareAndIncrementWorkerCount(c))
break retry;
c = ctl.get(); // Re-read ctl
if (runStateOf(c) != rs)
continue retry;
// else CAS failed due to workerCount change; retry inner loop
}
}
//启动worker
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
//ThreadExecutor的全局锁,在创建\销毁worker工作池的时候,才会用到
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// Recheck while holding lock.
// Back out on ThreadFactory failure or if
// shut down before lock acquired.
int rs = runStateOf(ctl.get());
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
workers.add(w);
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
if (workerAdded) {
t.start();
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
分析:addworker分为两部分:1、创建worker,2、启动worker
规则校验:
与core和maxPool数量的规则相同
创建worker:
获取ThreadLocal的全局锁。 安全的创建Worker。
t.start();
因此:重点又回到了Worker的run方法上
-
Worker.run()
public void run() {
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);
}
}
分析:这里就比较清晰了:
1、通过getTask()方法,获取待执行的任务。
2、通过task.run();执行具体的任务。
3、正常情况,只有当所有任务执行完毕才会停止运行。
因此:
1、进一步分析getTask()
2、执行task.run()方法。-->>这里可以看出,事实上线程在执行任务的时候,本质上是调用了任务自身的run/call方法。
==》》有点像是thread.get(threadlocal) 本质上是调用了 threadlocalMap.get(thread) 的感觉
-
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);
// Are workers subject to culling?
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;
}
}
}
分析:也不用把代码完全细节完全深究,可以发现方法是从workQueue中获取task的,所以最终的问题就是看这个变量workQueue是谁的成员变量。
public class ThreadPoolExecutor extends AbstractExecutorService {
private final BlockingQueue<Runnable> workQueue;
。。。
}
分析,getTask是从线程池中,获取的任务。即所有的任务都放在ThreadPoolExecutor中,线程池启动多个Worker去执行任务,每个worker不停的从ThreadPoolExector的workQueue中取出任务,比你高执行task.run()方法,直至所有的任务执行完毕。
至此分析完毕。
总结
阻塞队列 BlockingQueue
与线程池绑定,负责存放所有的待执行任务。
N个线程(core,Max)
本质上是指N个Worker对象
执行:
1、ThreadPoolExeuctor.execute();
根据规则,创建工人Worker,将提交的任务,添加任务至阻塞队列
2、Worker
真正执行任务的方法,不停的循环,直至所有的任务执行完毕,或者exit
3、阻塞队列 BlockingQueue
存放所有的待执行任务
说明
1、本文的目的是,解释如何实现线程复用,提交给多线程的任务是如何被执行的。基于这个目的出发,本文已经完成了目标。
2、因此,本文不是完整的介绍线程池,因此对许多知识点没有深究。