How is Java Timer implemented by the computer? -
the articles on site related timer talk how use timer program.
i ask different question. how java perform timer method?
since said avoid time-consuming work not use while loop check whether current time required time point, think timer not implemented using while loop continuously checking , comparing current time desired time point.
thank you!
i think timer not implemented using while loop continuously checking , comparing current time desired time point.
yes, is. optimization is; using priority queue based on nextexecutiontime tasks.
javadoc states
timer object single background thread used execute of timer's tasks, sequentially. timer tasks should complete quickly. if timer task takes excessive time complete, "hogs" timer's task execution thread. can, in turn, delay execution of subsequent tasks
timer class contains
taskqueue
priority queue of timertasks, ordered on nextexecutiontime.timerthread(queue)
timer's task execution thread, waits (queue.wait()
) tasks on timer queue.
timerthread
has private void mainloop() {
continuous while(true)
keep checking tasks comparing nextexecutiontime
currenttimemillis
currenttime = system.currenttimemillis(); executiontime = task.nextexecutiontime; if (taskfired = (executiontime<=currenttime)) {
and if reaches calling
if (taskfired) // task fired; run it, holding no locks task.run();
Comments
Post a Comment