跳转到相应内容

最常访问

最近访问

navigation

Specifying the Code to Run on a Thread

This lesson teaches you to

  1. Define a Class that Implements Runnable
  2. Implement the run() Method

You should also read

Try it out

Download the sample

ThreadSample.zip

This lesson shows you how to implement a 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.

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.

Define a Class that Implements Runnable

Implementing a class that implements Runnable is straightforward. For example:

public class PhotoDecodeRunnable implements Runnable {
    ...
    @Override
    public void run() {
        /*
         * Code you want to run on the thread goes here
         */
        ...
    }
    ...
}

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.

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.

You should also store a reference to the Runnable object's Thread in the Runnable itself, by calling Thread.currentThread().

The following snippet shows how to set up the run() method:

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 PhotoTask instance,
         * so that the instance
         * can interrupt the Thread.
         */
        mPhotoTask.setImageDecodeThread(Thread.currentThread());
        ...
    }
...
}
此网站会使用 Cookie 来存储您在此网站上指定的语言和显示选项偏好设置。

获取最新的 Android Developers 资讯和提示,助您在 Google Play 上取得成功。

* 必填字段

成功!

在微信上关注 Google Developers

要以浏览此网站吗?

您请求访问的是网页,但是您为此网站设置的语言偏好为

要更改您的语言偏好设置并以浏览此网站吗?如果以后您想要更改语言偏好设置,请使用每个页面底部的语言菜单。

该类需要 或更高的 API 级别

此文档已被隐藏,因为您为该文档选择的 API 级别是 。您可以使用左侧导航栏上方的选择器来更改文档的 API 级别。

要详细了解如何根据您的应用需求指定 API 级别,请参阅支持不同平台版本

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)