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

  1. taskqueue priority queue of timertasks, ordered on nextexecutiontime.
  2. 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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -