线程池之ThreadPoolExecutor源码解析

心已赠人 2023-08-17 17:03 215阅读 0赞

1.变量

ThreadPoolExecutor先定义了这几个常量,初看时一脸懵逼,其实它就是用int的二进制高三位来表示线程池的状态,

先回顾一下位运算:

  1. <<’左移:右边空出的位置补0,其值相当于乘以2。
  2. ‘>>’右移:左边空出的位,如果是正数则补0,若为负数则补0或1,取决于所用的计算机系统OS X中补1。其值相当于除以2。
  3. 负数二进制由它的绝对值取反后加1

    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));

    1. private static final int COUNT_BITS = Integer.SIZE - 3;// 29
    2. private static final int CAPACITY = (1 << COUNT_BITS) - 1; // 1<<29=00100000 00000000 00000000 00000000 再减1=00011111 11111111 11111111 11111111

       // 1的二进制是001,取反是110,再加1是111,111就是-1的二进制,再左移29,

    1. private static final int RUNNING = -1 << COUNT_BITS;// 11100000 00000000 00000000 00000000
    2. private static final int SHUTDOWN = 0 << COUNT_BITS;// 00000000 00000000 00000000 00000000
    3. private static final int STOP = 1 << COUNT_BITS;// 00100000 00000000 00000000 00000000
    4. private static final int TIDYING = 2 << COUNT_BITS;// 01000000 00000000 00000000 00000000
    5. private static final int TERMINATED = 3 << COUNT_BITS;// 01100000 00000000 00000000 00000000

  

转载于:https://www.cnblogs.com/akaneblog/p/11462549.html

发表评论

表情:
评论列表 (有 0 条评论,215人围观)

还没有评论,来说两句吧...

相关阅读