Java timer principles

Source: Internet
Author: User

Copyright Disclaimer: During reprinting, please use hyperlinks to indicate the original source and author information of the article and this statement
Http://comdean.blogbus.com/logs/4697030.html

When talking about how to implement a timer (not a clock), the first idea is to compile the program. The X86 platform can refer to the principles of this microcomputer. What if I want to implement it in Java? Since Java runs on the JVM, the instruction set provided by the virtual machine may have a solution. Interrupt handling? This is my first thought.
But when I saw the Timer class code, I found that Josh Bloch was very different from my idea. He used Java APIs to implement it. It was just an ordinary class, it is completely high to high, and it does not involve any mysterious underlying commands.
The whole mechanism is based on Java's multithreading technology.
For example, we use an alarm clock to get up regularly every morning. We only consider the ideal situation here. Except for the alarm clock, we won't hear any sound from the alarm clock, because when someone hears the mechanical watch, they won't be able to sleep. In addition to our operation, the alarm clock, otherwise, we won't be affected, so we don't have to worry about being kicked off the bed at night.
In this case, when we go to bed, the alarm clock is strictly in our own way, even if it breaks down, it has nothing to do with our sleeping. However, we will not go to bed because of the timing of the alarm. When the specified time is reached in the morning, the alarm will wake us up.
This is the principle of Java timers. In Java, the root class Object provides the wait (long timeout) method. This method will cause a thread to block the specified Timeout time. We seem to have a bit of thought here. When thread a is specified and the object B. Wait (timeout) is called, thread A will be blocked until the timeout reaches, and B will wake up to make thread a continue execution. In fact, this is the principle. The specific implementation of timer is more complicated.
You may have some questions:
1. I don't see multithreading. Only a is a thread, and B is an object.
2. Isn't there a sleep () method in the Thread class? It can be accurate to milliseconds. Why not?
The first problem is relatively simple. In fact, the Timer class is designed for multi-task timing. in implementation, B is a task queue (implementation is an array ), maintains all the tasks scheduled to use the current timer. They are a bunch of genuine thread instances. Each time thread a takes the scheduled task closest to the current time in the queue, compares it with the current time, and then wait (timeout) the time. The time when the thread wakes up is also the time when the scheduled task is running in the queue. Then the thread continues to take the next scheduled task and continues wait (timeout ). From this we can see that each time there is an additional time overhead, such as maintaining the queue, so the Timer class of Java is not guaranteed to be real-time.
The second problem is actually quite simple. After the sleep (timeout) method of the thread class is called, the thread is not actually sleep and will still be scheduled by the scheduler. Only interrupt () is used () the method can interrupt the sleep () method. In this way, the sleep () method is not really a sleep, and the wait method will make the thread sleep, until it is awakened, interrupted, or the parameter time is exhausted.
Looking at Java, wait () seems to be a bit strange. The wait () method is implemented in the root class object, that is to say, all Java classes inherit the wait () method, not just the Thread class. However, the wait () method is defined as universal, but only used in multithreading. The reason is also very simple. The lock in the critical section should not specify the object type. The wait () method of the lock should be used to block the entire thread. In other places, the lock should be wake up logically to wake up the entire thread, is a reasonable and correct choice.
Here we will talk about the implementation principle and want to draw a specific execution process, but multi-threaded execution is not easy to describe. It is the best way to look at the source code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.