线程池需要用到java的线程池类ThreadPoolExecutor,通常使用它的构造方法来创建线程池。
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:核心线程数,即线程池中保持存活的最少线程数。
核心线程即使空闲,也会一直存活,除非手动设置参数 allowCoreThreadTimeOut 为true,才能使空闲的核心线程在超过存活时间阈值后被销毁。
maximumPoolSize:线程池中允许存在的最大线程数。
keepAliveTime:空闲线程的存活时间。默认情况下,核心线程数一直保持存活,此参数只影响针对超出核心数的那部分线程;但如果设置了线程池属性 allowCoreThreadTimeOut 为true,则包括核心线程在内的所有线程均受keepAliveTime影响。
unit:keepAliveTime参数的时间单元。
workQueue:阻塞队列,用来保存待执行的任务。该队列类型为BlockingQueue,只能保存 execute 方法提交的 Runnable 类型的任务。
threadFactory:用来创建线程的线程工厂。可以不指定,默认会自动创建一个。
handler:拒绝策略,指线程池饱和,没有能力再处理或保存新任务时,采取的响应措施。可以不指定,默认为AbortPolicy类型,拒绝新任务并抛异常。
线程池工作流程
源码学习
在看ThreadPoolExecutor类的源码之前,有必要了解下类中的一些关键属性和方法:
/*
ctl是表示线程池控制状态的主要变量,是一个原子整数,按位拆分用来表示两个概念:
runState,表示线程池状态,占据高3位;workerCount,表示线程数量,占据低29位。
*/
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
/*
ctl的高3位表示线程池状态
RUNNING:接收新任务以及处理队列中的任务
SHUTDOWN:不接受新任务,仅处理队列中剩余的任务;比如调用shutdown()方法
STOP:不接受新任务,也不处理队列中的任务,并且会中断正在进行中的任务;比如调用shutdownNow()方法
TIDYING:所有任务都已终止,并且线程数为0;向该状态过渡时会调用terminated()钩子方法
TERMINATED:terminated()运行完成后的状态,即终止态。
*/
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; }
execute(Runnable command)
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);
}
addWorker(Runnable firstTask, boolean core)
private boolean addWorker(Runnable firstTask, boolean core) {
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);
//入参core为true表示以核心线程数为参考,fasle则表示以最大线程数为参考
//如果线程数已经超了,直接返回false,创建失败
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
//CAS操作修改工作线程数,如果成功则跳出retry这个标签
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;
//由于workers这个HashSet的线程不安全,操作需要加锁
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;
}
//工作线程Worker的构造方法,内部new了一个Thread对象,入参就是worker本身,也就是说线程启动后执行的run方法是Worker类的run方法。
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this);
}
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);
}
}
worker的run方法中,拿到自己的第一个任务并执行,执行完后继续循环地从阻塞队列中获取任务(getTask)并执行。
ThreadPoolExecutor中的阻塞队列workQueue类型为 BlockingQueue<Runnable>接口类型,具体的实现取决于我们所选择的实现类;这里以示例代码中使用的 LinkedBlockingQueue为例来分析阻塞队列的实现。
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?
//判断是否受到keepAliveTime空闲存活时间的影响。
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
//如果受keepAliveTime时间限制,则调用带参数的poll方法,否则调用take方法
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS)
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
E x = null;
int c = -1;
long nanos = unit.toNanos(timeout);
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
/*
LinkedBlockingQueue中有两把ReentrantLock锁,一把是takeLock,负责出队列操作的线程同步,一把是putLock,负责入队操作的线程同步。
另外,与这两把锁对应的,还有两个Condition,用来控制等待和唤醒:
Condition notEmpty = takeLock.newCondition();
Condition notFull = putLock.newCondition();
这里任务出队,需要申请takeLock
*/
takeLock.lockInterruptibly();
try {
/*
先循环判断阻塞队列中的任务数,如果为0,且剩余时间不足,表示当前线程的空闲时间已经超过keepAliveTime,直接返回null;
如果队列中没有任务,且时间还没到,则使当前线程进入超时等待状态,直到被唤醒/中断或超时。
*/
while (count.get() == 0) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
//如果发现阻塞队列中有任务,则从队列头部取出一个任务,队列中任务数减1
x = dequeue();
c = count.getAndDecrement();
/*
如果取出之后,队列中还有剩余的任务,则通知一个因为发现队列中没有任务而调用notEmpty.awaitNanos,然后释放锁并进入等待状态的线
程(当然也可能没有),告诉它当前队列不是空的,可以来取任务了。
*/
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
/*
如果取走任务之前,阻塞队列是满的,说明可能已经有线程(生产者)因为无法放入任务而阻塞了,需要唤醒(虽然在ThreadPoolExecutor中不存在
这种阻塞场景,但是LinkedBlockingQueue的使用场景有很多,不止ThreadPoolExecutor)
*/
if (c == capacity)
signalNotFull();
return x;
}
workQueue.take()
public E take() throws InterruptedException {
E x;
int c = -1;
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
while (count.get() == 0) {
notEmpty.await();
}
x = dequeue();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}
take方法和之前带参数的poll方法的主要区别在于,由于队列为空导致线程进入的等待状态是没有时间限制的,只能通过唤醒或中断来解除。
如果线程数小于核心线程数,并且addWorker方法返回true,则线程池的execute方法结束;否则尝试将任务放入阻塞队列。
workQueue.offer(command)
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
final AtomicInteger count = this.count;
if (count.get() == capacity)
return false;
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock;
//获取putLock锁
putLock.lock();
try {
//如果队列没有满,则入队,入队后队列还没有满,则唤醒一个因为队列满而阻塞住的线程
if (count.get() < capacity) {
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
}
} finally {
putLock.unlock();
}
//如果放入任务之前,阻塞队列是空的,说明可能已经有线程(消费者)因为无法取出任务而阻塞了,需要唤醒。
if (c == 0)
signalNotEmpty();
return c >= 0;
}
如果队列已满导致任务进入队列失败,则会尝试创建新的线程来执行任务,如果此时线程池没满,则能够创建,但是如果线程池已满,即线程数等于最大线程数,则创建线程失败,最终调用reject(command)执行拒绝策略。
线程池中自带的四种拒绝策略如下:
- AbortPolicy,默认的拒绝策略,拒绝任务,直接抛异常。
- CallerRunsPolicy,由提交任务的线程,也就是execute方法的调用者来执行任务。
- DiscardPolicy,什么都不做,等于把任务丢弃掉了。
- DiscardOldestPolicy,丢弃掉队列中等待时间最长的任务(队列头部的那个),然后接收当前任务。
源码如下:
//AbortPolicy
public static class AbortPolicy implements RejectedExecutionHandler {
public AbortPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
//CallerRunsPolicy
public static class CallerRunsPolicy implements RejectedExecutionHandler {
public CallerRunsPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
//DiscardPolicy
public static class DiscardPolicy implements RejectedExecutionHandler {
public DiscardPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
//DiscardOldestPolicy
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
public DiscardOldestPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}