If I schedule an event sometime after the program starts, it won't say that its anticipating an event UNLESS I uncomment the code on line 11 (else System.out.println("[" + now() + "ms] In statis...")
). What is going on with the thread?
public class Scheduler extends Thread {
private final Queue<ScheduledEvent> actions = new LinkedList<>();
private boolean running = true;
@Override
public void run() {
while (running) {
ScheduledEvent nextEvent = actions.peek();
if (nextEvent != null) {
System.out.println("[" + now() + "ms] Anticipating event at: " + nextEvent.time + "ms");
} //else System.out.println("[" + now() + "ms] In statis...");
if (nextEvent != null && nextEvent.time <= now())
actions.poll().execute();
}
}
private long now() {
return System.currentTimeMillis();
}
public ScheduledEvent schedule(long delay, Runnable action) {
return schedule(ScheduledEvent.delayedEvent(delay, action));
}
public ScheduledEvent scheduleAt(long time, Runnable action) {
return schedule(ScheduledEvent.absoluteEvent(time, action));
}
public ScheduledEvent schedule(ScheduledEvent event) {
System.out.println("Scheduled event at: " + event.time);
actions.add(event);
return event;
}
public void unschedule(ScheduledEvent event) {
actions.remove(event);
}
public void quit() {
running = false;
}
}
I wrote a class to schedule events and run them after/at a given time (in milliseconds). If I schedule an event right as the program starts, it will continue to run and eventually execute the scheduled event. I added System.out.println("exiting!");
to the end of the run()
function but it never outputs exiting!
.
After some digging, I found this issue which seemed to fix the issue. However, I don't understand why this is happening and why where(!this.isInterrupted())
fixed the issue.