Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Java Timer implemented by the computer?

Tags:

java

timer

The articles on the site related to Timer talk about how to use Timer to program.

I ask a different question. How does Java perform Timer method?

Since it is said to avoid time-consuming work by not to use while loop to check whether the current time is the required time point, I think Timer is not implemented simply by using while loop to continuously checking and comparing the current time to the desired time point.

Thank you!

like image 444
user1914692 Avatar asked May 18 '13 23:05

user1914692


People also ask

How does a Timer work in Java?

A Timer in Java is a process that enables threads to schedule tasks for later execution. Scheduling is done by keeping a specific process in the queue such that when the execution time comes, the processor can suspend other processes and run the task.

Is there a Timer class in Java?

A Java. util. Timer is a utility class used to schedule a task to be executed after a specific amount of time. Tasks can be scheduled for one-time execution or for repeated execution periodically.

What is Java util Timer?

The java. util. Timer class provides facility for threads to schedule tasks for future execution in a background thread. This class is thread-safe i.e multiple threads can share a single Timer object without the need for external synchronization.


2 Answers

I think Timer is not implemented simply by using while loop to continuously checking and comparing the current time to the desired time point.

YES, IT IS. The only optimization is; it is using priority queue based on nextExecutionTime for tasks.

JavaDoc states

Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks

Timer class contains

  1. TaskQueue which is a priority queue of TimerTasks, ordered on nextExecutionTime.
  2. TimerThread(queue) the timer's task execution thread, which waits (queue.wait()) for tasks on the timer queue.

TimerThread has private void mainLoop() {
where continuous while(true) will keep checking the tasks by comparing nextExecutionTime with currentTimeMillis

                    currentTime = System.currentTimeMillis();
                    executionTime = task.nextExecutionTime;
                    if (taskFired = (executionTime<=currentTime)) {

and if it reaches then calling

            if (taskFired)  // Task fired; run it, holding no locks
                task.run();
like image 175
Kanagavelu Sugumar Avatar answered Oct 24 '22 05:10

Kanagavelu Sugumar


According for the javadoc

This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method.

If you look in the code you will find a method called main loop. The first couple of lines are copied below.

private void mainLoop() {
    while (true) {
        try {

And... it uses a while loop inside of it along with Object.wait() to do the waiting.

like image 22
FDinoff Avatar answered Oct 24 '22 03:10

FDinoff