After submitting a Callable
to your ThreadPoolTaskExecutor
you should get a Future
. And on this Future
, you can call the get(long timeout, TimeUnit unit)
function with a TimeUnit
, which is the timeout, the maximum time the program will wait until either the future delivers or moves on, by throwing a TimeoutException
.
ie (unconfirmed pseudocode)
Future myFuture = threadPoolTaskExecutor.submit(myCallable);
try {
myResult = myFuture.get(5l,TimeUnit.SECONDS);
} catch(TimeoutException e) {
// Timeout-Related stuff here
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…