目录
Java线程池源码解析
Executor顶级接口
ExecutorService接口
AbstractExecutorService抽象类
ThreadPoolExecutor
Executors工具类
自己实现一个简单的线程池
Java线程池源码解析
代码中的@Override为我自己加的,为了方便知道是重写的方法
Executor顶级接口
public interface Executor {
void execute(Runnable command);
}
ExecutorService接口
public interface ExecutorService extends Executor {
void shutdown();
List<Runnable> shutdownNow();
boolean isShutdown();
boolean isTerminated();
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
// 主要看这些
<T> Future<T> submit(Callable<T> task);
<T> Future<T> submit(Runnable task, T result);
Future<?> submit(Runnable task);
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException;
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException;
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException;
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
AbstractExecutorService抽象类
public abstract class AbstractExecutorService implements ExecutorService {
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
// 将runnable包装成Callable
return new FutureTask<T>(runnable, value);
}
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new FutureTask<T>(callable);
}
// 以下三个方法都调用了execute()具体的执行方法
@Override
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}
@Override
public <T> Future<T> submit(Runnable task, T result) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task, result);
execute(ftask);
return ftask;
}
@Override
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException {
...
}
...
}
ThreadPoolExecutor
public class ThreadPoolExecutor extends AbstractExecutorService {
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;
}
// Executors.newFixedThreadPool调用这个方法
/**
* newFixedThreadPool调用这个方法将指定固定的线程
* new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
// 默认的线程工厂和默认的错误句柄处理
Executors.defaultThreadFactory(), defaultHandler);
}
private static final RejectedExecutionHandler defaultHandler =
new AbortPolicy();
// rejectedExecution方法总是会抛出异常,用于无法执行任务时,将调用这个对象的方法抛出异常
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
*/
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
重要成员变量 begin
/**
* AtomicInteger类型的ctl代表了ThreadPoolExecutor中的控制状态,
* 它是一个复核类型的成员变量,是一个原子整数,借助高低位包装了两个概念:
(1)workerCount:线程池中当前活动的线程数量,占据ctl的低29位;
(2)runState:线程池运行状态,占据ctl的高3位,
有RUNNING、SHUTDOWN、STOP、TIDYING、TERMINATED五种状态。
*/
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static final int COUNT_BITS = Integer.SIZE - 3;// 32 - 3
/**
* 运算过程为1左移29位,也就是00000000 00000000 00000000 00000001
-->
001 0000 00000000 00000000 00000000,
再减去1的话,就是
000 11111 11111111 11111111 11111111,
前三位代表线程池运行状态runState,
所以这里workerCount的理论最大值就应该是29个1,即536870911;
*/
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
/**
* 线程池运行状态runState,它占据ctl的高3位
runState提供主要的生命周期控制,并具有以下值:
RUNNING:接受新任务并处理排队的任务
SHUTDOWN:不接受新任务,但处理排队的任务
STOP:不接受新任务,不处理排队的任务,并中断进行中的任务
TIDYING:所有任务都已终止,workerCount为零,
线程转换到TIDYING状态将运行Terminated()挂钩方法
TERMINATED:terminald()已完成这些值之间的数值顺序很重要,可以进行有序比较。
*/
// runState is stored in the high-order bits
/**
* -1在Java底层是由32个1表示的,左移29位的话,即111 00000 00000000 00000000 00000000,
也就是低29位全部为0,高3位全部为1的话,表示RUNNING状态,即-536870912;
*/
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; }
重要成员变量 end
// 只有运行态是负数,因此isRunning(int c)方法,只需要判断是否小于SHUTDOWN即可。
private static boolean isRunning(int c) {
return c < SHUTDOWN;
}
// 重要
// 实现了顶级接口Executor的唯一方法
// 在调用ExecutorService.submit()方法时,
// 父类AbstractExecutorService重写了submit()方法,
// 方法中会调用顶级接口的Executor.execute()方法。
// 总结:这个方法围绕着addWorker()和reject()方法做控制,要嘛成功执行任务,要嘛抛异常
@Override
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
/*
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/
/**
* 分3个步骤进行:
* 1.如果正在运行的线程少于corePoolSize线程,
请尝试使用给定命令作为其第一个任务来启动新线程。
对addWorker的调用从原子上检查runState和workerCount,
从而通过返回false来防止在不应该添加线程的情况下发出错误警报。
* 2.如果任务可以成功排队,
那么我们仍然需要仔细检查是否应该添加线程(因为现有线程自上次检查后就死掉了),
或者自进入此方法以来该池已关闭。
因此,我们重新检查状态,并在必要时回滚排队(如果已停止),
或者在没有线程的情况下启动新线程。
* 3.如果我们无法将任务排队,则尝试添加一个新线程。如果失败,我们知道我们已关闭或已饱和,
因此拒绝该任务。
*/
int c = ctl.get();
// 如果c中有效线程数目小于corePoolSize大小,尝试添加新的worker线程处理任务command:
// 从c中获取有效线程数目调用的是workerCountOf()方法,
// 添加新的worker线程处理任务command调用的是addWorker()方法,
// 线程数的判断利用corePoolSize作为边界约束条件
// 方法返回值是标志添加worker是否成功的标志位,ture表示成功,false表示失败,
// 如果为true,则直接返回,否则重新获取ctl的值c
// 总结:如果当前工作线程小于核心数就尝试启动新的线程
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
// 总结:如果当前工作线程大于或等于核心数,或者启动新线程失败时,
// 根据rs线程池状态运行态,则将任务丢入worker队列;
// 非运行态,则尝试启动,若失败调用reject句柄
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方法前半块就是从线程池的状态、线程数量、并发安全考虑当前线程池是不是具备添加一个新线程的条件
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
// 第一层for循环作用是,从线程池状态层面判断,如果符合条件直接返回false
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循环作用是,使用乐观锁抢修改wc线程数的权利,如果修改成功则跳出循环
for (;;) {
int wc = workerCountOf(c);
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
if (compareAndIncrementWorkerCount(c))
// 如果进入了这个循环就说明当前没有并发情况,则跳出两层for死循环
break retry;
// 走到这里说明上面的break是没有被执行,也就是说上面存在的并发
// 如果这个时候线程池的状态和之前不一样了,发生了改变,
// 安全起见重新回到retry(重新执行最外层的for循环)
// 如果状态没有改变就继续内层的for循环来重新争夺改变WorkerCount的机会
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;
}
// Worker线程继承Runnable
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable
{
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
// 根据线程工厂的定义指定新线程的组以及线程方法名
this.thread = getThreadFactory().newThread(this);
}
/** Delegates main run loop to outer runWorker */
@Override
public void run() {
runWorker(this);
}
...
}
// 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 {
// 执行w的任务,若为空则从worker队列中取任务,执行run()方法
/**
* getTask方法中,没有任务则会阻塞或等待超时
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
*/
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);
}
}
}
Executors工具类
public class Executors {
// 固定线程数的线程池
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
// 默认的线程工厂
public static ThreadFactory defaultThreadFactory() {
return new DefaultThreadFactory();
}
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
// Java安全管理器
// 当运行未知的Java程序的时候,该程序可能有恶意代码(删除系统文件、重启系统等)
// 为了防止运行恶意代码对系统产生影响,需要对运行的代码的权限进行控制,
// 这时候就要启用Java安全管理器
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
}
参考:
线程池的套路解析,非常详细!
ThreadPoolExecutor源码分析(一):重要成员变量
ThreadPoolExecutor源码分析(二):任务提交主逻辑execute()
自己实现一个简单的线程池
MyThreadPool.java
package top.senseiliu.threadpool;
public interface MyThreadPool {
void execute(Runnable r);
}
MyThreadPoolImpl.java
package top.senseiliu.threadpool;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class MyThreadPoolImpl implements MyThreadPool {
private static final int DEFAULT_SIZE = 10;
private BlockingQueue<Runnable> workerQueue;
private int corePoolSize = DEFAULT_SIZE;
public MyThreadPoolImpl(int corePoolSize) {
if (corePoolSize <= 0) {
this.corePoolSize = this.DEFAULT_SIZE;
} else {
this.corePoolSize = corePoolSize;
}
this.workerQueue = new LinkedBlockingQueue<Runnable>();
init();
}
public MyThreadPoolImpl() {
this(DEFAULT_SIZE);
}
private void init() {
// 初始化线程池时,启动corePoolSize个线程
for (int i = 0; i < corePoolSize; ++i) {
Worker w = new Worker();
w.start();
}
}
private final class Worker extends Thread {
@Override
public void run() {
while (true) {
try {
// 启动时打印线程名字
System.out.println("线程ID" + Thread.currentThread().getId() + "已启动");
// 阻塞方法,从队列中取任务,并执行
Runnable r = workerQueue.take();
r.run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void execute(Runnable r) {
try {
workerQueue.put(r);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试
@Test
public void myThreadPoolTest() {
final int COUNT = 10000;
MyThreadPool myThreadPool = new MyThreadPoolImpl();
myThreadPool.execute(() -> {
for (int i = 0; i < COUNT; ++i) {
System.out.println("线程ID" + Thread.currentThread().getId() + ":" + i);
}
});
myThreadPool.execute(() -> {
for (int i = 0; i < COUNT; ++i) {
System.out.println("线程ID" + Thread.currentThread().getId() + ":" + i);
}
});
/**
* Out
*
* 线程ID11已启动
* 线程ID12已启动
* 线程ID13已启动
* 线程ID14已启动
* 线程ID15已启动
* 线程ID16已启动
* 线程ID20已启动
* 线程ID19已启动
* 线程ID18已启动
* 线程ID17已启动
* ...
* 线程ID11:23
* 线程ID11:24
* 线程ID12:0
* 线程ID11:25
* 线程ID12:1
* 线程ID11:26
* 线程ID12:2
* 线程ID11:27
* 线程ID12:3
* 线程ID12:4
* ...
*
* */
}
如需转载,请注明出处
Java线程池源码解析及自己实现一个简单的线程池——魔法海螺