Android timer, recommended Scheduledthreadpoolexecutor

Source: Internet
Author: User

Android timer, recommended Scheduledthreadpoolexecutor

Official website: http://developer.android.com/reference/java/util/Timer.html

To implement the function of the timer, there are several methods:
1.Thread 's sleep(long) method
2,Handler postdelayed(Runnable, Long) method
3. The method of combiningTimer and timertask
These 3 methods, essentially the same, are thread-related; Here's a timer.

The timer can have a name, or it may not.

Timers, from the thread type angle, can be divided into the following two kinds:
1, ordinary type;
2, Backstage (Deamon) type;
Timer ()---No name, normal type
Timer (String name)---has a name, normal type
Timer (Boolean Isdaemon)---No name, can specify type
The Timer (String name, Boolean Isdaemon)---has a name that can specify the type
See also: Daemon threads (Daemon thread)

Timers, from the angle of execution times, can be divided into the following two kinds:
1. Execute only once; this, like the next two member functions:
Schedule (timertask task, Date when)
Schedule (timertask task, long delay)
2. Cyclic execution; The following four member functions are like this one:
Schedule (timertask task, Date when, long period)
Schedule (timertask task, long delay, long period)
Scheduleatfixedrate (timertask task, long delay, long period)
Scheduleatfixedrate (timertask task, Date when, long period)
The first two, ensure that the execution interval is stable, two, to ensure that the execution time is stable.

Because the timer cannot guarantee the real-time (real-time) characteristic;
In other words, it is possible that an execution is too time-consuming to cause the timer to be executed immediately.
As a result, when it comes to this "already timed out, it's time to run the timer" case, there are two scenarios:
1, guarantee the execution interval is stable, namely Scheduledexecutetime (n+1) =realexecutetime (nth time) +periodtime)
2, to ensure that the execution time is stable, that is scheduledexecutetime (n+1 times) =firstexecutetime +n*periodtime

Timer, you can also cancel (cancel ()) and Clean (purge (), i.e. clear all canceled)

Before the JDK1.5, the timer/cycle operation was implemented by the timer. But the timer has several dangers:
1, the timer is based on absolute time. Easily affected by the system clock.
2. The timer only creates a new thread to perform all timetask, and all timetask may be affected.
3, the timer will not catch TimerTask exception, but simply stop, this will affect the execution of other timetask.
If you use more than JDK1.5, it is recommended to replace the timer with Scheduledthreadpoolexecutor .
It takes relative time and uses the thread pool to execute timertask, which handles timertask exceptions.
It basically solves all of the above problems.

Android timer, recommended Scheduledthreadpoolexecutor

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.