Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anmol-Singh-Jaggi/c9e942358b7f4453839c07762aabd079 to your computer and use it in GitHub Desktop.
Save Anmol-Singh-Jaggi/c9e942358b7f4453839c07762aabd079 to your computer and use it in GitHub Desktop.
blog scheduler approach 2
void goToSleep(job, jobQueue){
jobQueue.push(job);
sleep(job.nextExecutionTime() - getCurrentTime());
}
void executeJob(job, jobQueue){
threadpool.submit(job); // async call
job = job.copy();
job.setNextExecutionTime(getCurrentTime() + job.getExecutionInterval());
jobQueue.add(job);
}
@Override
void run(){
while(true)
{
job = jobQueue.pop()
if(job.nextExecutionTime() > getCurrentTime()){
// Nothing to do
goToSleep(job, jobQueue)
}
else{
executeJob(job, jobQueue)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment