The working principle of Android--timer and timertask and the specific meanings of its related functions

Source: Internet
Author: User

Principle:

Its basic processing model is a single-threaded scheduling task queue model, the timer constantly accept scheduling tasks, all tasks accept timer scheduling after adding Taskqueue,timerthread constantly go to taskqueue task to execute.

It is not difficult to see from the diagram, this is a producer-consumer model of a special case: Multi-producer, single-consumer model.

This type of Message Queuing implementation also has a similar implementation in the programming model in the browser, and the timed execution function settimeout (expression,milliseconds) in JavaScript is also implemented based on this principle.

The disadvantage of this approach is that when a task executes for a long time, it exceeds the time that the next task in Taskqueue starts executing, and it affects the real-time performance of the entire task. In order to improve the real-time, we can use multiple consumers to spend together to improve the processing efficiency and avoid the realization of such problems.

See http://www.cnblogs.com/jinspire/archive/2012/02/10/2345256.html for specific explanations.

Visible TimerTask can be seen as just an event, while a timer is a queue

1.Timer

    1. Java.util.Timer Timer = new Java.util.Timer (true);
    2. True indicates that the timer runs in daemon mode (Low priority,
    3. Program End timer also end automatically), note that javax.swing
    4. There is also a timer class in the package, if you use the Swing package in import,
    5. Pay attention to the clash of names.
    6. TimerTask task = new TimerTask () {
    7. Public void Run () {
    8. ...   //each time the code needs to be executed is placed in this area.
    9. }
    10. };
    11. Here are a few ways to schedule a task:
    12. Timer.schedule (task, time);
    13. Time is a date type: Executed once at the specified times.
    14. Timer.schedule (Task, firsttime, period);
    15. Firsttime is a date type and period is a long
    16. This is done every period milliseconds from the firsttime moment.
    17. Timer.schedule (task, delay)
    18. Delay is a long type: it is executed once from now through delay milliseconds
    19. Timer.schedule (task, delay, period)
    20. Delay is long,period to long: From now on delay milliseconds, every period
    21. Milliseconds to execute once.
    22. Timer.cancel ()//cancels the current timer and all the tasks in that queue (the running task is not affected), no more tasks are added to the queue, and then the timer does nothing
    23. Timer.purge ()//Remove the canceled task from the queue so that they can be recycled as garbage

2.TimerTask functions of some common functions

Timertask.cancel ()//cancels the current task and moves it out of the queue

The working principle of Android--timer and timertask and the specific meanings of its related functions

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.