I think the timer is not implemented simply, using a while loop to continuously check and compare the current time with the desired time point.
YES THIS. The only optimization; it uses a priority queue based on nextExecutionTime for tasks.
JavaDoc Status
A timer object is a single background thread that is used to perform all timer tasks in sequence. Timer tasks should complete quickly. If the timer task takes too long to complete, it starts the timer task execution thread. This may, in turn, delay the execution of subsequent tasks.
Timer class contains
TaskQueue , which is a priority queue of TimerTasks ordered in nextExecutionTime.TimerThread(queue) timer task execution thread that waits for ( queue.wait() ) tasks in the timer queue.
TimerThread has private void mainLoop() {
where continuous while(true) will continue to check tasks by comparing nextExecutionTime with currentTimeMillis
currentTime = System.currentTimeMillis(); executionTime = task.nextExecutionTime; if (taskFired = (executionTime<=currentTime)) {
and if he reaches, then the challenge
if (taskFired) // Task fired; run it, holding no locks task.run();
Kanagavelu sugumar
source share