I am running an infinite loop and need to achieve the following steps:
- check available threads in the executor service (the infinite loop)
- fetches the task in the loop.
- execute the task in the background(non-blocking) and kill the thread executing the task if it takes more than 3 seconds.
I have looked into the future get API that takes a timeout parameter but not this is blocking in nature.
while(any thread available in thread pool){
Task task = fetchTask();
// somehow execute this task in a non-blocking fashion with a timeout.
}
Is there a way to kill the asynchronously executing threads after the timeout? Will the thread execution will stop and resources will be freed after the timeout?