使用ThreadPoolTaskExecutor
时是否可以使任务超时?我无法将 ThreadPoolTaskExecutor 更改为 ThreadPoolExecutor 或 ExecutorService。
最佳答案
向您的ThreadPoolTaskExecutor
提交Callable
后,您应该得到一个Future
。而在这个Future
上,你可以调用get(long timeout, TimeUnit unit)
函数,并带有一个TimeUnit
,它是超时,最大程序将通过抛出 TimeoutException
等待,直到 future 交付或继续运行。
ie(未经证实的伪代码)
Future myFuture = threadPoolTaskExecutor.submit(myCallable);
try {
myResult = myFuture.get(5l,TimeUnit.SECONDS);
} catch(TimeoutException e) {
// Timeout-Related stuff here
}
关于java - 如何使用 ThreadPoolTaskExecutor 设置任务超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41099778/