美文网首页
深入分析java线程池的实现原理

深入分析java线程池的实现原理

作者: jerrik | 来源:发表于2017-12-25 11:08 被阅读0次
    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; }
    

    https://mp.weixin.qq.com/s/qyRWTx47jpqtLh7QPfRNOw

    相关文章

      网友评论

          本文标题:深入分析java线程池的实现原理

          本文链接:https://www.haomeiwen.com/subject/iwxlgxtx.html