final ExecutorService service = Executors.newSingleThreadExecutor();
Callable<String> theCode = new Callable<String>() {
public String call() {}
};
try {
final Future<?> future = service.submit (theCode);
sReturn = (String) future.get(4, TimeUnit.SECONDS);
}
catch (InterruptedException | ExecutionException | TimeoutException e) {
logger.warning("fail." + e.getMessage() + e.toString());
}
finally {
service.shutdown();
}
callable或runnable,使代码运行在另一个线程,主线程中计时控制而已,抛出timeout后theCode还可以继续运行直至结束,因为是两个不同的线程。
暂存未验证