码迷,mamicode.com
首页 > 编程语言 > 详细

JDK线程池框架Executor源码阅读

时间:2016-06-25 16:20:10      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

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源码阅读

标签:

原文地址:http://www.cnblogs.com/qquan/p/5616373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!