ThreadPoolExecutor 클래스를 이해하려고합니다. 그 클래스에서 선언 된 일부 최종 변수를 발견하여 사용법을 이해할 수 없습니다.ThreadPoolExecutor의 workerCountOf() 메소드 java
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
위의 내용은 최종 변수와 2 진수 및 10 진수 값입니다.
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; }
앞서 방법의 코멘트, 내가 관찰 출력 :
은 그럼 사용하여 두 가지 방법이 varibles을 발견했다. 이제 ThreadPoolExecutor#execute(runnable) 방법,
그것은,If fewer than corePoolSize threads are running
int c = ctl.get();
if (workerCountOf(c) < corePoolSize)
이해하려고로 문을 다음과 같은 계산을하는 경우 workerCountOf (C)의 값보다 클 수 있습니다
corePoolSize. ctl의 초기 값은 RUNNING입니다. 의 inc하는 방법과 원자하는 dec CTL 값이있는 또한
,
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
, 실행, 말할 수
,
에도다음 workerCountOf(ctl.get()) = 0
, ((RUNNING+5) & CAPACITY) = 0
으로
.
누구나 내게이 최종 변수를 작성한 이유와 사용법을 설명 할 수 있습니까?
workerCountOf() 메서드가 실제로 실행중인 스레드를 반환하지 않는 이유는 무엇입니까?
나는 뭔가를 놓쳤을 것입니다.
감사
고마워요. 또한이 클래스가 이진 표현을 어떻게 활용하는지 이해하게 해줘서 고마워. –
@ akash777.sharma, 내 기쁨 –
당신은 황소 눈을 잡았습니다. –