public Runnable getNextTask() {
        try {
            while (true) {
                synchronized (tasks) {
                    Iterator<Entry> iter = tasks.iterator();
                    if (!iter.hasNext()) {
                        if (shutdown) {
                            return null;
                        }
                        // Set flag for unit tests to coordinate with.
                        waiting = true;
                        tasks.wait();
                        waiting = false;
                        continue;
                    }
                    Entry retEntry = iter.next();
                    long timeNow = timer.milliTime();
                    if (retEntry.getTimestamp() > timeNow) {
                        waiting = true;
                        tasks.wait(retEntry.getTimestamp() - timeNow);
                        waiting = false;
                        continue;
                    }
                    iter.remove();
                    return retEntry.getTask();
                }
            }
        } catch (InterruptedException e) {
            return null;
        }
    }
Waits until the queue has a task that is ready for scheduling, removes that
task from the queue and returns it.

@return The next task.
documentapi/src/main/java/com/yahoo/documentapi/messagebus/ScheduledEventQueue.java