private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));//11100000000000000000000000000000
private static final int COUNT_BITS = Integer.SIZE - 3;//29
private static final int CAPACITY = (1 << COUNT_BITS) - 1;//00100000000000000000000000000000(2^29) -1 => 00011111111111111111111111111111
// runState is stored in the high-order bits
//根据补码做移位运算
private static final int RUNNING = -1 << COUNT_BITS;//[111000000000000000000000000000000] => 10000000000000000000000000000001(原码) -> 11111111111111111111111111111111(补码) -> 11100000000000000000000000000000(左移29位)
private static final int SHUTDOWN = 0 << COUNT_BITS;//00000000000000000000000000000000
private static final int STOP = 1 << COUNT_BITS;//00100000000000000000000000000000
private static final int TIDYING = 2 << COUNT_BITS;//01000000000000000000000000000000
private static final int TERMINATED = 3 << COUNT_BITS;//01100000000000000000000000000000
//查看高3位(线程状态) ~CAPACITY = 11100000000000000000000000000000
private static int runStateOf(int c) { return c & ~CAPACITY; }
//计算低29位(线程数)
private static int workerCountOf(int c) { return c & CAPACITY; }
private static int ctlOf(int rs, int wc) { return rs | wc; }
深入分析java线程池的实现原理
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 简书 占小狼[https://www.jianshu.com/users/90ab66c248e6/latest_...
- 以前讲过,画画的目的不一有很多,有的是天赋,有的是无聊,有的是喜欢,有的是功利,但是到最后很多都不了了之。 现在越...