Future.cancel (false)은 실제로 작업 실행을 방지 할 수있는 경우에만 true를 반환해야합니다.Future.cancel (false) 실행중인 작업에 대해 true를 반환합니다.
그러나 아래 코드에서 우리는 그것이 모순 된 것을 알 수 있습니다. 작업이이 "Not expecting this statement!!!"
public class Test {
public static void main(String[] args) throws InterruptedException {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
final boolean[] canceled = { false };
ScheduledFuture<?> future = executor.schedule(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
if (canceled[0])
System.out.println("Not expecting this statement!!!");
} catch (InterruptedException e) {
}
}
}, 0, TimeUnit.SECONDS);
Thread.sleep(100);
canceled[0] = future.cancel(false);
executor.shutdown();
executor.awaitTermination(2, TimeUnit.SECONDS);
}
}
을 인쇄하지 말아야 canceld됨에 따라
는불행하게도이 프로그램의 출력은 는 "이 문을 기대하지!"입니다
일부 사용자는이 동작을 설명 할 수 있습니까?
내 경우에는 true를 반환합니다. 질문이 제기됩니다. –
주 방법에서 수면 시간을 늘려보십시오. 당신은 다른 대답을 볼 수 있습니다 – prasanth
이 질문에 +1을 준 사람은 처음 15 초 만에 +1을 받았습니다 ... –