I 폭 (10)의 고정 스레드 풀 ExecutorService
, 100 Callable
's의 목록, 각각 20 초 동안 대기하고 자신의 인터럽트 기록이있다.자바 ExecutorService를 invokeAll을() 중단
나는이 목록을 별도의 스레드에서 호출하고 거의 즉시이 스레드를 인터럽트하려고합니다. ExecutorService
실행이 예상대로 중단되지만 Callable
으로 기록 된 실제 인터럽트 수는 10-20-40 정도입니다. ExecutorService
이 10 개 이상의 스레드를 동시에 실행할 수없는 경우 그 이유는 무엇입니까?
전체 소스 :
@Test
public void interrupt3() throws Exception{
int callableNum = 100;
int executorThreadNum = 10;
final AtomicInteger interruptCounter = new AtomicInteger(0);
final ExecutorService executorService = Executors.newFixedThreadPool(executorThreadNum);
final List <Callable <Object>> executeds = new ArrayList <Callable <Object>>();
for (int i = 0; i < callableNum; ++i) {
executeds.add(new Waiter(interruptCounter));
}
Thread watcher = new Thread(new Runnable() {
@Override
public void run(){
try {
executorService.invokeAll(executeds);
} catch(InterruptedException ex) {
// NOOP
}
}
});
watcher.start();
Thread.sleep(200);
watcher.interrupt();
Thread.sleep(200);
assertEquals(10, interruptCounter.get());
}
// This class just waits for 20 seconds, recording it's interrupts
private class Waiter implements Callable <Object> {
private AtomicInteger interruptCounter;
public Waiter(AtomicInteger interruptCounter){
this.interruptCounter = interruptCounter;
}
@Override
public Object call() throws Exception{
try {
Thread.sleep(20000);
} catch(InterruptedException ex) {
interruptCounter.getAndIncrement();
}
return null;
}
}
사용 WINXP 32 비트 오라클 JRE 1.6.0_27 및 JUnit4
나는 가설에 동의하지
흠 ... 주된 방법으로 프로그램으로 변환하면, 나는 항상 10을 얻고있다. (Windows 7의 Java 7) –
동일한 결과는 37 (1.6.0_27, Windows XP)이다. 테스트 할 Java 7이 없으므로 다른 사람이 확인할 수 있습니까? –
직장에서 해보겠습니다. 어쩌면 자바 6 버그 일 수도 ... –