Parse the usage and instances of Timer in Java and TimerTask in Android

  • 2020-05-16 07:01:03
  • OfStack

In development we sometimes have the need to perform a task at regular intervals. For example, if the controls on UI need to change over time, we can use the timer utility classes provided by Java, namely Timer and TimerTask.

Timer is a common class with several important methods; While TimerTask is an abstract class, there is an abstract method run(), similar to the run() method in the thread. We use Timer to create one of its objects, and then use the schedule method of this object to complete the operation of this interval.

The schedule method has three parameters

The first parameter is an object of type TimerTask. The run() method we implement TimerTask is a task to be performed periodically.

The second parameter has two types: the first is of type long, indicating how long it will take to start execution; the second is of type Date, indicating how long it will start execution.

The third parameter is the period of execution, of type long.

The schedule method also has an execution overload of two parameters, the first parameter is still TimerTask, the second parameter is in the form of long to indicate how long it takes to execute once, and Date to indicate how long it takes to execute once.

Timer is one thread, schedule method is used to complete the scheduling of TimerTask, multiple TimerTask can share one Timer, that is to say, Timer object calls schedule method once is to create a thread, and after calling schedule once TimerTask is unlimited loop, cancel() of Timer is used to stop operation. Of course, after executing the cancel() method once with an Timer, all Timer threads are terminated.

Usage:


//true  Illustrate this timer In order to daemon Mode run (low priority, end of program timer Also automatically ends)  
java.util.Timer timer = new java.util.Timer(true); 
 
TimerTask task = new TimerTask() { 
 public void run() { 
 // Every time you need to execute code you put it in here.  
 } 
}; 
 
// Here are several schedules task Method:  
 
//time for Date Type: executes at a specified time 1 Times.  
timer.schedule(task, time); 
 
//firstTime for Date type ,period for long , said from the firstTime Start at all times, every once in a while period Milliseconds to perform 1 Times.  
timer.schedule(task, firstTime, period); 
 
//delay  for long Type: from now on delay Milliseconds to perform 1 Times.  
timer.schedule(task, delay); 
 
//delay for long,period for long From now on delay Milliseconds later, every once in a while period Milliseconds to perform 1 Times.  
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/*  said 1000 Milliseconds later, every once in a while 1000 Ms � line 1 time  */); 
 } 
 
 /** 
 * 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 articles: