【发布时间】:2014-12-28 13:12:21
【问题描述】:
我有这个代码:
public <T> T executeCodeBlockWithTimeLimit(String criticalBlockTimeOutMilli, Callable<T> callable) throws
Exception {
ExecutorService service = Executors.newSingleThreadExecutor();
Future<T> future = service.submit(callable);
startStopWatch();
T result;
if (criticalBlockTimeOutMilli != null) {
result = future.get(Long.parseLong(criticalBlockTimeOutMilli), TimeUnit.MILLISECONDS);
} else {
result = callable.call();
}
long timeElapsed = stopStopWatch();
e2eResult.runTime = timeElapsed;
service.shutdown();
return result;
}
在外部方法中:
Callable<Void> callable = new
Callable<Void>() {
@Override
public Void call() throws
Exception {
System.out.println("22222");
return null;
}
};
timerUtils.executeCodeBlockWithTimeLimit(criticalBlockTimeOutMilli, callable);
我看到控制台打印了
22222
22222
为什么要调用两次?
【问题讨论】:
-
criticalBlockTimeOutMilli 是否设置为空?
标签: java multithreading callable executor