Is it possible to timeout a task when using ThreadPoolTaskExecutor
? I cannot change the ThreadPoolTaskExecutor
to ThreadPoolExecutor
or to ExecutorService
.
Look into thread interruption. Thread. stop() it!
The default configuration of core pool size is 1, max pool size and queue capacity as 2147483647. This is roughly equivalent to Executors.
Returns the thread keep-alive time, which is the amount of time which threads in excess of the core pool size may remain idle before being terminated.
The corePoolSize is the minimum number of workers to keep alive without timing out. It is a configurable property of ThreadPoolTaskExecutor. However, the ThreadPoolTaskExecutor abstraction delegates setting this value to the underlying java. util.
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
}
Refer my below Git hub link for TimeOutThreadPoolTaskExecutor
https://github.com/vivek-gupta-21563/timeoutthreadpool
you can execute or submit a task with preferred time out parameters
execute(() -> System.out.println("Task to execute"), 2, TimeUnit.Minute);
submit(() -> System.out.println("Task to execute"), 2, TimeUnit.Minute);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With