Timers is mainly used to run some tasks in the background. You can set timer as the daemon thread. When cancel is called, all scheduled tasks are not canceled.
Tasks in timer are executed sequentially. If a task takes a long time to complete, it may affect the start time of the next task.
Timertask does not guarantee the real-time scheduling of tasks, because the underlying implementation relies on the object. Wait (long) method.
Multiple Threads can share a timer that is not synchronized for themselves.
Timer has two scheduling modes: fixed-rate (fixed scheduling cycle) and fixed-period (complete execution cycle ). The default value is fixed-period.
Fixed-period:
public void schedule (TimerTask task, long delay, long period) Schedule a task for repeated fixed-delay execution after a specific delay. schedule (TimerTask task, Date when, long period)
The first execution starts after the delay time, and then runs again at intervals of period. If the time is reached, but other tasks are currently being executed,
It can be scheduled and executed only when the current task is finished.
Fixed-rate:
public void scheduleAtFixedRate (TimerTask task, long delay, long period) scheduleAtFixedRate (TimerTask task, Date when, long period)
The first execution starts after the delay time. After that, the execution starts at intervals of period. Even if other tasks are being executed, the task is scheduled and executed.
Note: The daemon thread is a thread that is always running and will not stop even if the task is completed.
Deep understanding of timer writing methods
Many programmers will think that the implementation method of Android timer is relatively simple, but it is not so hasty. Here we will introduce you to the relevant knowledge.
The Android mobile operating system is an open-source mobile operating system based on the Linux platform. For programmers, it should be easy to modify the operating system. However, we still need to pay attention to many details in the middle. For example, a developer familiar with Java may think that android timer should be used as follows:
The program function is very simple: 10 seconds later, settitle is "Hear me ?", Then, in fact, this does not work, which is related to the thread security of Android!
Java code
package com.ray.test; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; public class JavaTimer extends Activity { Timer timer = new Timer(); TimerTask task = new TimerTask(){ public void run() { setTitle("hear me?"); } }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timer.schedule(task, 10000); } }
But the actual usage should be implemented by using handler to implement the android timer function!
Java code
package com.ray.test; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class TestTimer extends Activity { Timer timer = new Timer(); Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 1: setTitle("hear me?"); break; } super.handleMessage(msg); } }; TimerTask task = new TimerTask(){ public void run() { Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timer.schedule(task, 10000); } }