Android Timer TimerTask Usage notes

Source: Internet
Author: User

Android often encounters tasks that perform periodically and regularly. Beginners often use the Thread.Sleep () method. In Android, there is a timer that can be dedicated to doing this.

Let's see what's in Timer.class. You should know when you see the first few lines of code .... There is a static inner class in the timer: Timerimpltimerimpl has a static inner class: Timerheaptimerimpe inherits the thread, which is used to implement the specific task execution content. Timerheap defines an array of timertask that is used to manage allocations for multiple timertask. The advantage of using static inner classes is that multiple timer objects can be pooled. Here's a look at timertask.class. Well, more simple than a timer, is a runnable implementation. Just a few more objects Lock: Object lock, when the timer in the TimerTask operation, ensure synchronization, thread safety. Cancelled: Flag variable, whether the current timertask has been canceled. When:timer when executing timertask, how much time is delayed. Period:timer the cycle time when the timertask is executed. Fixedrate: Whether a fixed frequency is executed, such as a timertask, the execution period is 3 seconds, maybe a task is not completed in 3 seconds, Fixedtate is true, then the next task will still be executed. If it is Fasle, the next task waits until the previous execution is complete and continues the cycle execution. The way to use it is simple:
The last line of code: Timer.schedule (task,1000,3000) means that a task is executed with a period of 3 seconds after 1 seconds.

Android Timer TimerTask Usage notes

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.