JDK线程池框架Executor源码阅读
Posted Q_Quan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK线程池框架Executor源码阅读相关的知识,希望对你有一定的参考价值。
Executor框架
Executor
ExecutorService
AbstractExecutorService
ThreadPoolExecutor
ThreadPoolExecutor继承AbstractExecutorService,是一个线程池的具体的实现
主要成员
1. ctl
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;
AtomicInteger类型的私有变量ctl,使用高3bit表示线程池状态runState,低29bit来表示线程池线程总数。可见,Executor线程数量是有限制的,最多(2^29)-1个(about 500 million)。
前3bit表示线程池生命周期的5个状态,RNUNING,SHUTDOWN,STOP,TIDYING,TERMINATED。注释中给出了线程池所有可能的状态转化,随着生命周期的变更,几个状态从小到大赋值。
2. workQueue
private final BlockingQueue<Runnable> workQueue;
存储待执行tasks的任务队列。
3. workers
private final HashSet<Worker> workers = new HashSet<Worker>();
工作线程集合。
4. mainLock
private final ReentrantLock mainLock = new ReentrantLock();
访问workers时必须持有的锁。
主要方法
总结
ScheduleExecutorService
Executors
总结
以上是关于JDK线程池框架Executor源码阅读的主要内容,如果未能解决你的问题,请参考以下文章