ThreadPoolExecutor java中的workerCountOf()方法 [英] workerCountOf() method in ThreadPoolExecutor java

查看:456
本文介绍了ThreadPoolExecutor java中的workerCountOf()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解 ThreadPoolExecutor 类.我发现该类中声明了一些最终变量,但无法理解它们的用途.

I am trying to understand ThreadPoolExecutor class.I found some final variables declared in that class and unable to understand their use.

private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static final int COUNT_BITS = Integer.SIZE - 3;         //29
private static final int CAPACITY   = (1 << COUNT_BITS) - 1;    //536870911     00011111111111111111111111111111

// RUN_STATE is stored in the high-order bits
private static final int RUNNING    = -1 << COUNT_BITS;         //-536870912    11100000000000000000000000000000
private static final int SHUTDOWN   =  0 << COUNT_BITS;         //0             00000000000000000000000000000000
private static final int STOP       =  1 << COUNT_BITS;         //536870912     00100000000000000000000000000000
private static final int TIDYING    =  2 << COUNT_BITS;         //1073741824    01000000000000000000000000000000
private static final int TERMINATED =  3 << COUNT_BITS;         //1610612736    01100000000000000000000000000000

以上是最终变量及其二进制和十进制值.

Above are the final variables and their binary and decimal values.

然后我发现了两种使用这些变量的方法:

Then I found two methods with use these varibles :

private static int runStateOf(int c)     { return c & ~CAPACITY; }  // RUN_STATE & ~CAPACITY = RUN_STATE
private static int workerCountOf(int c)  { return c & CAPACITY; }   // RUN_STATE & CAPACITY = 0
private static int ctlOf(int rs, int wc) { return rs | wc; }

方法前面的注释是我观察到的输出.

The comments ahead of the methods, are the output that I observed.

现在在 ThreadPoolExecutor#execute(runnable)方法,

它正在使用以下语句进行以下计算:如果正在运行的线程数少于 corePoolSize

It is doing following calculation with a statement as If fewer than corePoolSize threads are running

int c = ctl.get();
if (workerCountOf(c) < corePoolSize)

我试图理解,在这种情况下,workerCountOf(c) 的值可能大于 corePoolSize.可以看到ctl的初始值为RUNNING.

I am trying to understand, in which case the value of workerCountOf(c) can be greater than corePoolSize. As you can see the initial value of ctl is RUNNING.

此外,还有一些方法可以原子地 inc 和 dec ctl 值,

Also , there are methods to inc and dec ctl values atomically ,

private boolean compareAndIncrementWorkerCount(int expect) {
    return ctl.compareAndSet(expect, expect + 1);
}

private boolean compareAndDecrementWorkerCount(int expect) {
    return ctl.compareAndSet(expect, expect - 1);
}

现在假设有 5 个线程正在运行,所以 ctl = RUNNING + 5,

Now lets say , 5 threads are running, so ctl = RUNNING + 5,

即使这样 workerCountOf(ctl.get()) = 0 ,

作为 ((RUNNING+5) & CAPACITY) = 0.

谁能解释一下创建这些最终变量的原因及其用途?

Can anybody explain me the reason of creating these final variable and their use?

workerCountOf() 方法实际上如何不返回正在运行的线程?

How workerCountOf() method actually returning no of running threads?

我一定是遗漏了什么.

谢谢

推荐答案

如您所见,Java 使用 int ctl 字段来存储池的当前状态和线程数.状态存储在高三位,所有其他位用于存储线程数.位掩码 CAPACITY 用于将它们彼此分开:

As you can see, Java uses the int ctl field to store both the current state of the pool and the number of threads. The state is stored in the higher three bits and all other bits are used to store the number of threads. Bitwise mask CAPACITY is used to separate them from each other:

  • 容量 = 000111111111111111111111111111111
  • ~容量 = 11100000000000000000000000000000

因此,

  • ctl &CAPACITY 保留低 29 位并将高 3 位归零;结果是当前线程数
  • ctl &~CAPACITY 保留较高的三位并将所有其他位归零;结果是池运行状态
  • ctl & CAPACITY preserves the lower 29 bits and zeroes the higher three bits; the result is the current threads number
  • ctl & ~CAPACITY preserves the higher three bits and zeroes all the others; the result is the pool run state

正如您正确注意到的,具有五个线程的运行池具有 ctl = (RUNNING + 5),它具有二进制表示 111000...000101.因此,应用 CAPACITY 掩码将最高三位归零,并为您提供值 000000...000101,即 5,而不是 0.

As you correctly noticed, the running pool with five threads has ctl = (RUNNING + 5), that has binary representation 111000...000101. So, applying the CAPACITY mask zeroes the highest three bits and gives you the value 000000...000101, which is 5, not 0.

这篇关于ThreadPoolExecutor java中的workerCountOf()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆