Timertask 或 Handler

假设我希望每10秒执行一些操作,并且不一定需要更新视图。

问题是: 对 timertask 使用 timer 是否更好(我的意思是更有效率和效果) ,比如:

final Handler handler = new Handler();


TimerTask timertask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
<some task>
}
});
}
};
timer = new Timer();
timer.schedule(timertask, 0, 15000);
}

或者只是一个延期的管理员

final Handler handler = new Handler();
final Runnable r = new Runnable()
{
public void run()
{
<some task>
}
};
handler.postDelayed(r, 15000);

另外,如果您能解释何时使用哪种方法,以及为什么其中一种方法比另一种方法更有效率(如果确实如此的话) ,我将不胜感激。

74755 次浏览

There are some disadvantages of using Timer

It creates only single thread to execute the tasks and if a task takes too long to run, other tasks suffer. It does not handle exceptions thrown by tasks and thread just terminates, which affects other scheduled tasks and they are never run

Copied from:

TimerTask vs Thread.sleep vs Handler postDelayed - most accurate to call function every N milliseconds?

Handler is better than TimerTask.

The Java TimerTask and the Android Handler both allow you to schedule delayed and repeated tasks on background threads. However, the literature overwhelmingly recommends using Handler over TimerTask in Android (see here, here, here, here, here, and here).

Some of reported problems with TimerTask include:

  • Can't update the UI thread
  • Memory leaks
  • Unreliable (doesn't always work)
  • Long running tasks can interfere with the next scheduled event

Example

The best source for all kinds of Android examples that I have seen is at Codepath. Here is a Handler example from there for a repeating task.

// Create the Handler object (on the main thread by default)
Handler handler = new Handler();
// Define the code block to be executed
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
// Do something here on the main thread
Log.d("Handlers", "Called on main thread");
// Repeat this the same runnable code block again another 2 seconds
handler.postDelayed(runnableCode, 2000);
}
};
// Start the initial runnable task by posting through the handler
handler.post(runnableCode);

Related

Kotlin version of accepted answer:

// execute on the main thread, empty constructor is deprecated
val handler = Handler(Looper.getMainLooper())


val runnableCode = object : Runnable {
override fun run() {
Log.d("Handlers", "Called on main thread")
handler.postDelayed(this, 2000)
}
}


// or shorter using a lambda function
val runnableCode = Runnable {
Log.d("Handlers", "Called on main thread")
handler.postDelayed(this, 2000)
}


handler.post(runnableCode)