Future.get(timeout) は、指定されたタイムアウト後に TimeoutException を確実にスローしません。これは正常な動作ですか、それとも信頼性を高めるために何かできることはありますか? このテストは私のマシンでは失敗します。ただし、2000 ではなく 3000 でスリープすると合格します。
public class FutureTimeoutTest {
@Test
public void test() throws
ExecutionException,
InterruptedException {
ExecutorService exec = Executors.newSingleThreadExecutor();
final Callable call = new Callable() {
@Override
public Object call() throws Exception {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
return 0;
}
};
final Future future = exec.submit(call);
try {
future.get(1000, TimeUnit.MILLISECONDS);
fail("expected TimeoutException");
} catch (TimeoutException ignore) {
}
}
}