Usage of Timer and TimerTask in Java in Android

Source: Internet
Author: User

In development, we sometimes need to execute a task at a fixed interval. For example, if the controls on the UI need to change over time, we can use the Timer tool class provided by Java, namely Timer and TimerTask.

Timer is a common class with several important methods. TimerTask is an abstract class with an abstract method run (), similar to the run () method in a thread, we use Timer to create an object, and then use the schedule method of this object to complete the operation at this interval.

The schedule method has three parameters.
The first parameter is an object of the TimerTask type. The run () method of TimerTask is a task to be executed cyclically;
The second parameter can be of two types. The first parameter is of the long type, indicating how long the execution starts and the other parameter is of the Date type, indicating that the execution starts after that time;
The third parameter is the execution cycle, which is of the long type.

The schedule method also has a two-parameter execution overload. The first parameter is still TimerTask, and the second parameter is in the long format to indicate how long the execution will take, if it is set to Date, the task is executed once after a certain time.

Timer is a thread that uses the schedule method to schedule TimerTask. Multiple timertasks can share one Timer. That is to say, a thread is created when the Timer object calls the schedule method once, in addition, after a schedule call, TimerTask runs in an unrestricted loop and uses the cancel () function of Timer to stop the operation. Of course, after the same Timer executes the cancel () method once, all Timer threads are terminated.

Usage:

// True indicates that the timer runs in daemon mode (the priority is low, and the timer ends automatically after the program ends) java. util. timer timer = new java. util. timer (true); TimerTask task = new TimerTask () {public void run () {// the code to be executed each time is put here. }}; // The following methods are used to schedule tasks: // time is of the Date type: Run Once at the specified time. Timer. schedule (task, time); // the firstTime is of the Date type, and the period is long, which indicates that the task is executed every time of the specified period in milliseconds. Timer. schedule (task, firstTime, period); // delay is of the long TYPE: delay is executed once in milliseconds from now on. Timer. schedule (task, delay); // The value of delay is long and the period is long. From now on, after delay is milliseconds, it is executed every second in the period. Timer. schedule (task, delay, period );

Sample Code:

Import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import java. util. timer; import java. util. timerTask; public class TimerTaskActivity extends Activity {private Timer mTimer; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // init timer mTimer = new Timer (); // start timer task setTimerTask () ;}@ Override protected void onDestroy () {super. onDestroy (); // cancel timer mTimer. cancel ();} private void setTimerTask () {mTimer. schedule (new TimerTask () {@ Override public void run () {Message message = new Message (); message. what = 1; doActionHandler. sendMessage (message) ;}, 1000,100 0/* indicates that after 1000 milliseconds, the row is renewed every 1000 milliseconds */);} /*** do some action */private Handler doActionHandler = new Handler () {@ Override public void handleMessage (Message msg) {super. handleMessage (msg); int msgId = msg. what; switch (msgId) {case 1: // do some action break; default: break ;}}};}

Related Article

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.