In development, we sometimes have the need to perform a certain task at a fixed period of time. For example, the controls on the UI need to change over time, we can use the tool classes of timers provided by Java, namely Timer and TimerTask.
Timer is an ordinary 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 this interval operation.
Schedule method has three parameters
The first parameter is an object of type TimerTask. The run() method of implementing TimerTask is a task that needs to be executed periodically;
There are two types of the second parameter. The first is long type, which indicates how long it takes to start execution, and the other is Date type, which indicates that it starts execution after that time;
The third parameter is the execution cycle, which is of long type.
The schedule method also has a two-parameter execution overload. The first parameter is still TimerTask, and the second form represented as long indicates how long it will be executed after it is executed. For Date, it means that it will be executed after a certain time.
Timer is a thread, which uses the schedule method to complete the scheduling of TimerTask. Multiple TimerTasks can share a Timer. That is to say, the Timer object calls the schedule method once, which creates a thread. After calling the schedule once, TimerTask will loop unlimitedly, and use Timer's cancel() to stop the operation. Of course, after the same Timer executes the cancel() method once, all Timer threads are terminated.
usage:
//true means that this timer runs in daemon mode (low priority, the timer ends the program and ends automatically) java.util.Timer timer = new java.util.Timer(true); TimerTask task = new TimerTask() { public void run() { //Put the code that needs to be executed every time is put here. } }; //The following are several methods for scheduling tasks: //time is Date type: executed once at a specified time. timer.schedule(task, time); //firstTime is Date type, period is long, indicating that it is executed every milliseconds from the firstTime time. timer.schedule(task, firstTime, period); //delay is type long: delay is executed once every milliseconds from now on. timer.schedule(task, delay); //delay is long, period is long: after delay milliseconds are passed from now, execute every milliseconds. 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, 1000/* means that after 1000 milliseconds, line 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; } } }; }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.