Search code examples
javamultithreadingscheduled-tasksscheduledexecutorservicehibernation

How to tell ScheduledExecutorService to ignore overlapping executions after resuming computer from suspension/hibernation


I've got a thread in charge of periodically refreshing some remote service queries response cache every 10-15 minutes.

The problem is when a client's machine resumes from sleep the refresh task executes sleep time / refresh period times instead of just once.

It's not overloading the server or anything, but it is inefficient. How can I prevent this from happening?


Solution

  • Assuming the task is scheduled at a fixed rate via the ScheduledExecutorService, change the task to a self-scheduling task. In this manner there is always only 1 task waiting to be executed.