}
}
private long runAllDue() throws InterruptedException {
long nextRunTime = 0;
TimedTask task;
while ((task = scheduledTasks.poll(60, java.util.concurrent.TimeUnit.SECONDS)) != null) {
if (!task.isDue(currentTimeMillis())) {
long wait = task.nextRuntime() - currentTimeMillis();
if (wait > 0) {
try {
Thread.sleep(wait);
}
catch (InterruptedException e) {
if (stopped) return 0;
}
}
}
/**
* Sechedule the task for execution.
*/
if (!queue.offer(task, 5, java.util.concurrent.TimeUnit.SECONDS)) {
switch (saturationPolicy) {
case CallerRuns:
task.run();
break;
case Fail:
throw new RuntimeException("could not schedule task: queue is saturated");
}
}
if (task.calculateNextRuntime()) {
scheduledTasks.offer(task);
}
}
return nextRunTime;