J.U.C源码
Java线程池
前言
在Java后端程序开发的过程中,经常使用线程池技术来处理前端web并发请求,或者后端多个处理任务等。Java sdk中自带的ThreadPoolExecutor就是自带的线程池。
一、线程池作用
- 降低资源消耗
通过池化技术重复利用已经创建的线程,降低线程创建和销毁造成的损耗。 - 提高响应速度
任务到达时,无需等待线程创建即可立即执行。 - 提高线程的可管理性
线程是稀缺资源,使用线程次可以进行统一的分配、调优、和监控。 - 提供更多更强大的功能
Java sdk中自带的ThreadPoolExecutor具备可拓展性,允许开发人员向其中增加更多的功能。比如延时定时线程池 ScheduledThreadPoolExecutor,就允许任务延期执行或定期执行。
二、ThreadPoolExecutor
1.继承关系
2.运行状态
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static final int COUNT_BITS = Integer.SIZE - 3;
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// runState is stored in the high-order bits
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;
// Packing and unpacking ctl
private static int runStateOf(int c) { return c & ~CAPACITY; }
private static int workerCountOf(int c) { return c & CAPACITY; }
private static int ctlOf(int rs, int wc) { return rs | wc; }
ThreadPoolExecutor的ctl是整个实现类的关键,高3位,代表线程池的状态;低29位表示woker的数量。
- RUNNING:能够接受新提交的任务,也可以处理阻塞队列的任务
- SHUTDOWN:关闭状态,不能接受新提交的任务,但是可以继续处理正常处理的任务的线程
- STOP:不能接受新的任务,也不处理队列的任务,会中断正在处理任务的线程
- TIDYING:所有的任务都已经终止,workerCount为0
- TERMINATED:在terminated()方法执行完后进入该状态
下面是线程池状态转化流程:
3.ThreadPoolExecutror构造函数
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.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
- corePoolSize,核心线程数,当有新任务在execute()方法提交时,处理逻辑如下:
如果workerCount < corePoolSize,则创建并启动一个线程来执行新提交的任务;
如果 workerCount >= corePoolSize,且线程池内的阻塞队列未满,则将任务添加到该阻塞队列中;
如 果 workerCount >= corePoolSize && workerCount < maximumPoolSize,且线程池内的阻塞队列已满,则创建并启动一个线程来执行新提交的任务;
如果 workerCount >= maximumPoolSize,并且线程池内的阻塞队列已满 ,则根据拒绝策略来处理该任务 , 默认的处理方式是直接抛异常; - maximumPoolSize,最大线程数量
- keepAliveTime,线程池维护线程所允许的空闲时间
- unit,keepAliveTime时间单位
- workQueue,保存等待执行的任务的阻塞队列,队列可以分成如下几种:
直接切换队列:SynchronousQueue,一个不存储元素的阻塞队列,每一个put操作必须等待take操作,否则不能添加元素;
有界队列:ArrayBlockingQueue、LinkedBlockingQueue
无界队列:PriorityBlockingQueue、DelayQueue、LinkedTransferQueue、LinkedBlockingDeque - threadFactory,创建线程的工厂,建议不同的业务使用不同的名称前缀,后续出现问题,方便排查
- handler,线程池的拒绝策略,当线程池的任务队列已满,并且线程池中的线程数目达maximumPoolSize时,就需要拒绝任务。JDK默认提供4种拒绝策略:
ThreadPoolExecutor.AbortPolicy,丢弃任务并抛弃RejectedExecutionException移除。线程池的默认拒绝策略。比较关键的业务推荐使用此策略;
ThreadPoolExecutor.DiscardPolicy,丢弃任务,但是不抛出异常。使用此策略,无法发现系统的异常状态,建议一些无关紧要的业务采用此策略;
ThreadPoolExecutor.DiscardOldestPolicy,丢弃队列最前面的任务,然后重新提交被拒绝的任务;
ThreadPoolExecutor.CallerRunsPolicy,由调用线程(提交任务的线程)处理该任务
4.提交任务函数
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
// 获取线程池当前的状态,包括线程池的运行状态和woker线程的数量
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
// 若work线程数量是否小于核心线程数量,添加新的woker线程执行任务
if (addWorker(command, true))
return;
// 若添加worker线程失败,重新获取当前线程池的状态
c = ctl.get();
}
// 若线程池正在运行,则将任务添加到队列中
if (isRunning(c) && workQueue.offer(command)) {
// 重新获取当前线程池的状态
int recheck = ctl.get();
// 若线程池不是运行状态,则删除队列的任务,使用拒绝策略拒绝该任务
if (! isRunning(recheck) && remove(command))
reject(command);
// 若woker线程数量为0,则添加一个新的woker线程
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
// 若添加woker线程失败,则使用拒绝策略拒绝该任务
else if (!addWorker(command, false))
reject(command);
}
5.实际添加任务
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
// 获取线程池当前状态,包括运行状态,和woker线程数量
int c = ctl.get();
// 获取线程池的运行状态
int rs = runStateOf(c);
// 若线程池运行状态大于等于SHUTDOWN,除了当线程池的运行状态为SHUTDOWN时,添加新的worker线程,消费队列的任务的情况,返回false
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
for (;;) {
int wc = workerCountOf(c);
// 若woker线程数量大于CAPACITY,或者 核心线程数量/最大线程数量,则返回false
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
}
}
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
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;
}
6.Worker类
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable
{
/**
* This class will never be serialized, but we provide a
* serialVersionUID to suppress a javac warning.
*/
private static final long serialVersionUID = 6138294804551838833L;
/** Thread this worker is running in. Null if factory fails. */
final Thread thread;
/** Initial task to run. Possibly null. */
Runnable firstTask;
/** Per-thread task counter */
volatile long completedTasks;
/**
* Creates with given first task and thread from ThreadFactory.
* @param firstTask the first task (null if none)
*/
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this);
}
/** Delegates main run loop to outer runWorker */
public void run() {
runWorker(this);
}
// Lock methods
//
// The value 0 represents the unlocked state.
// The value 1 represents the locked state.
protected boolean isHeldExclusively() {
return getState() != 0;
}
protected boolean tryAcquire(int unused) {
if (compareAndSetState(0, 1)) {
setExclusiveOwnerThread(Thread.currentThread());
return true;
}
return false;
}
protected boolean tryRelease(int unused) {
setExclusiveOwnerThread(null);
setState(0);
return true;
}
public void lock() { acquire(1); }
public boolean tryLock() { return tryAcquire(1); }
public void unlock() { release(1); }
public boolean isLocked() { return isHeldExclusively(); }
void interruptIfStarted() {
Thread t;
if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
try {
t.interrupt();
} catch (SecurityException ignore) {
}
}
}
}
7.runWorker方法
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 ((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);
}
}
8.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;
}
}
}
三、总结
学以致用,了解Java线程池的目的就是更好的使用,线程池中核心线程数可以参考如下的方法:
- 《Java并发编程实践》
- 实践经验
上述的两种方式,都有失偏颇,最好的方法还是参照上述设置方法进行设置,然后再在现场进行调优。