5

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?

NotGaeL
  • 8,344
  • 5
  • 40
  • 70

1 Answers1

3

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.

Community
  • 1
  • 1
vanOekel
  • 6,358
  • 1
  • 21
  • 56
  • I had forgotten about this question, but yes; this is what I ended up doing. It is good to have some confirmation though, because I haven't got a lot of experience on this, and I was worried that scheduling this way could be a bad practice (something like: what if there is an abnormal termination I didn't account for, like not enough resources to execute the update thread, and the next update doesn't get scheduled?), and so I thought that maybe the standard practice was something different I wasn't finding anywhere because I didn't have a name for it. – NotGaeL Apr 25 '15 at 14:24