本文介绍了新的Runnable(),但没有新的线索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解这里的code ,特别是匿名类

I'm trying to understand the code here , specifically the anonymous class

private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
   final long start = mStartTime;
   long millis = SystemClock.uptimeMillis() - start;
   int seconds = (int) (millis / 1000);
   int minutes = seconds / 60;
   seconds     = seconds % 60;

   if (seconds < 10) {
       mTimeLabel.setText("" + minutes + ":0" + seconds);
   } else {
       mTimeLabel.setText("" + minutes + ":" + seconds);
   }

   mHandler.postAtTime(this,
           start + (((minutes * 60) + seconds + 1) * 1000));
   }
};

文章说

该处理器运行更新code作为你的主线程的一部分,避免了第二个线程的开销。

不应该创建一个新的运行的类进行新的第二个线程? Runnable接口的类的目的是什么在这里除了能够通过一个Runnable类postAtTime?

Shouldn't creating a new Runnable class make a new second thread? What is the purpose of the Runnable class here apart from being able to pass a Runnable class to postAtTime?

感谢

推荐答案

的Runnable 通常被用来提供code线程应该运行,但的Runnable 本身无关使用线程。这只是一个对象与的run()方法。

Runnable is often used to provide the code that a thread should run, but Runnable itself has nothing to do with threads. It's just an object with a run() method.

在Android上,处理程序类可用于要求框架的一样的线程上以后运行约code,而比的不同的一个。 的Runnable 用于提供code应该稍后运行。

In Android, the Handler class can be used to ask the framework to run some code later on the same thread, rather than on a different one. Runnable is used to provide the code that should run later.

这篇关于新的Runnable(),但没有新的线索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:21