Runnable
class, which runs the code in its Runnable.run()
method on a separate thread. You can also pass a Runnable
to another object that can then attach it to a thread and run it. One or more Runnable
objects that perform a particular operation are sometimes called a task.
本课程将向您展示如何实现Runnable类,这个类将会在一个线程中运行它的Runnable.run方法中的代码。当然,你也可以将
Runnable
传递给其他对象,这个其他对象可以讲自己附加在一个线程上,然后再线程中运行自己。一个或者多个Runnable
对象,它们执行一个特殊的操作时,也被称为任务。一个runnable就是一个任务,线程总要有一个任务才能跑起来。
Thread
and Runnable
are basic classes that, on their own, have only limited power. Instead, they're the basis of powerful Android classes such as HandlerThread
, AsyncTask
, and IntentService
. Thread
and Runnable
are also the basis of the class ThreadPoolExecutor
. This class automatically manages threads and task queues, and can even run multiple threads in parallel.
Thread和Runnable
都是基类,也就是说它们只有有限的功能。而且,它们还是安卓当中一些比较牛逼的类,比如HandlerThread
, AsyncTask
, and IntentService
它们的基类。Thread和Runnable也是ThreadPoolExecutor
的基类。ThreadPoolExecutor
自动管理线程和任务队列,甚至可以并行运行多个线程。
Define a Class that Implements Runnable
Implementing a class that implements Runnable
is straightforward. For example:
实现一个Runnable是很直观的,比如:
public class PhotoDecodeRunnable implements Runnable { ... @Override public void run() { /* * Code you want to run on the thread goes here */ ... } ... }
线程是一个客户,它只需要运行runnable接口,至于这个接口里面是什么任务,线程不管。因此,为线程提供任务的类,必须要实现runnable接口,也就是创建一个任务。
Implement the run() Method
In the class, the Runnable.run()
method contains the code that's executed. Usually, anything is allowable in a Runnable
. Remember, though, that the Runnable
won't be running on the UI thread, so it can't directly modify UI objects such as View
objects. To communicate with the UI thread, you have to use the techniques described in the lesson Communicate with the UI Thread.
Runnable.run()
方法包含要被执行的代码。通常,Runnable中任何东西都是允许的。记住,虽然
Runnable不会再主线程中运行,它不会直接修改UI对象,比如view对象。为了与UI线程通信,你必须要使用在Communicate with the UI Thread中谈到的技术。
At the beginning of the run()
method, set the thread to use background priority by calling Process.setThreadPriority()
with THREAD_PRIORITY_BACKGROUND
. This approach reduces resource competition between the Runnable
object's thread and the UI thread.
在run方法的开始处,配合THREAD_PRIORITY_BACKGROUND来调用
Process.setThreadPriority()
,以此来设置线程以使用背景优先级。这个方法可以减少运行runnable的线程与主线程之间争夺资源的竞争。
THREAD_PRIORITY_BACKGROUND
会使得线程的优先级略微低于正常的优先级,这样做的话,用户在于UI线程交互的时候,这个线程对UI线程响应用户的交互的影响就会小一点。
You should also store a reference to the Runnable
object's Thread
in the Runnable
itself, by calling Thread.currentThread()
.
你也应该在runnable中存储一个使用它的线程的引用,存储的方法就是调用 Thread.currentThread()
The following snippet shows how to set up the run()
method:
下面的例子显示了如何来设置run方法:
class PhotoDecodeRunnable implements Runnable { ... /* * Defines the code to run for this task. */ @Override public void run() { // Moves the current Thread into the background android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); ... /* * Stores the current Thread in the the PhotoTask instance, * so that the instance * can interrupt the Thread. */ 之所以保存该线程的一个引用,是为了后续可以取消这个线程的执行。
mPhotoTask.setImageDecodeThread(Thread.currentThread()); ... } ... }