Search code examples
javaconcurrencyexecutorserviceblockingqueue

ScheduledExecutorService with variable delay


Suppose I have a task that is pulling elements from a java.util.concurrent.BlockingQueue and processing them.

public void scheduleTask(int delay, TimeUnit timeUnit)
{
    scheduledExecutorService.scheduleWithFixedDelay(new Task(queue), 0, delay, timeUnit);
}

How can I schedule / reschedule the task if the frequency can be changed dynamically?

  • The idea is to take a stream of data updates and propagate them in batch to a GUI
  • The user should be able to vary the frequency of updates

Solution

  • I don't think you can change a fixed rate delay. I think you need to use schedule() to perform a one-shot, and schedule again once that has completed (with a modified time out if required).