Correct/Optimized way to reschedule a Java task in a ScheduledExecutorService?
Suppose that we have a Java Task (eg: Runnable
) that will be executed if we do not receive an external signal in x
seconds.
A common way to solve this problem is to use a schedule thread pool (such as ScheduledExecutorService
) to execute this task considering x
delay. When we receive that external event, from other part of the system, we need to reschedule this task, that is, to reinsert this task in the thread pool with x
delay.
My question is: what is the correct/optimized way to reschedule a task in a ScheduleExecutorService
? Current solutions usually focus on removing/canceling and reinserting the dealyed task in the thread pool. Is this the best approach?