I have a few tasks which are registered by
final ScheduledExecutorService ses = Executors.newScheduledThreadPool(10);
List<ScheduledFuture<?>> futures = new ArrayList<>();
tasks.forEach(task->{
var future = ses.scheduleWithFixedDelay(() -> run(task), 0, 3, TimeUnit.SECONDS);
futures.add(future);
});
// and now cancel all tasks one for one after 10 seconds..
ses.scheduleWithFixedDelay(() ->
{
log.info("cancel task----");
futures.get(0).cancel(false);
}, 0, 10, TimeUnit.SECONDS);
As you can see, for each task the futures
holds a task.getId()
so I can obtain the ScheduledFuture
of a task afterwards. I do not want to ses.shutdown()
because this will shutdown the whole schedulings for the other tasks as well, which I want to avoid.
The only solution I actually see is to create one ScheduledExecutorService
for each task to be able to shutdown it afterwards for a specified task, but then I cannot make use of the pooling.
How can I shutdown only a specified task within the pool?