Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 456
NotGaeL Avatar asked Oct 19 '22 14:10

NotGaeL


1 Answers

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.

like image 109
vanOekel Avatar answered Nov 01 '22 14:11

vanOekel