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.

本课程将向您展示如何实现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());
...
}
...
}

  

Specifying the Code to Run on a Thread的更多相关文章

  1. MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems

          早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...

  2. "setItem@[native code] logging run flush"

    safari 中出现 "setItem@[native code] logging run flush" 此问题出现在 6s plus ios系统为10.2 时, safari打开 ...

  3. DOMContentLoaded vs jQuery.ready vs onload, How To Decide When Your Code Should Run

    At a Glance Script tags have access to any element which appears before them in the HTML. jQuery.rea ...

  4. Android 线程池系列教程(2)Thread,Runnable是基类及如何写Run方法

    Specifying the Code to Run on a Thread 上一课   下一课 1.This lesson teaches you to Define a Class that Im ...

  5. Android Training

    Building Apps with Content Sharing Simple Data --> Intent && ActionProvider 介绍如何让应用程序共享简单 ...

  6. Android 线程池系列教程(1)目录

    Sending Operations to Multiple Threads 1.Dependencies and prerequisites Android 3.0 (API Level 11) o ...

  7. Sending Operations to Multiple Threads_翻译

    The speed and efficiency of a long-running, data-intensive operation often improves when you split i ...

  8. Thread的run()与start()的区别

    Java的线程是通过java.lang.Thread类来实现的.VM启动时会有一个由主方法所定义的线程.可以通过创建Thread的实例来创建新的线程.每个线程都是通过某个特定Thread对象所对应的方 ...

  9. Thread’s start method and run method

    工作中用到了Thread,一开始用错了,仔细研究了一下,稍作整理. 前言,今天写代码居然这样写的 new Thread() { @Override public void run() { System ...

随机推荐

  1. 起调UWP的几种方法

    原文:起调UWP的几种方法 由于种种原因吧,我需要使用一个WPF程序起调一个UWP程序,下面总结一下,给自己个备份. 启动UWP程序的关键是协议启动 给我们的UWP应用添加一个协议,like this ...

  2. Android 调试桥(adb)是多种用途的工具

    Android 调试桥 Android 调试桥(adb)是多种用途的工具,该工具可以帮助你你管理设备或模拟器 的状态. 可以通过下列几种方法加入adb: 在设备上运行shell命令 通过端口转发来管理 ...

  3. transform 遇上 position: fixed

    最近遇到一个有意思的现象,以下 demo 中 fixed 的元素没有相对 viewport 定位,而是相对于它的父元素进行定位. <html> <head> <style ...

  4. 浅谈.NET编译时注入(C#-->IL)

    原文:浅谈.NET编译时注入(C#-->IL) .NET是一门多语言平台,这是我们所众所周知的,其实现原理在于因为了MSIL(微软中间语言)的一种代码指令平台.所以.NET语言的编译就分为了两部 ...

  5. Attention is all you need及其在TTS中的应用Close to Human Quality TTS with Transformer和BERT

    论文地址:Attention is you need 序列编码 深度学习做NLP的方法,基本都是先将句子分词,然后每个词转化为对应的的词向量序列,每个句子都对应的是一个矩阵\(X=(x_1,x_2,. ...

  6. android核心系列--1,组件生命周期

    一,进程模型及进程托管 1,一个APP应用是由一个或多个组件构成的,这些组件可以运行在一个进程中,也可以分别运行在多个进程中: 进程的构造和销毁是由系统全权负责的. 2,一个应用进程只有一个应用环境对 ...

  7. SYN2306A型 GPS北斗双模授时板

    SYN2306A型 GPS北斗双模授时板 北斗gps时钟北斗授时设备北斗时钟同步系统使用说明视频链接: http://www.syn029.com/h-pd-211-0_310_36_-1.html ...

  8. 30255Java_5.5 GUI

    GUI GUI的各种元素(如:窗口,按钮,文本框等)由Java类来实现 1.AWT 使用AWT所涉及的类一般在java.awt包及其子包中 AWT(Abstract Window Toolkit)包括 ...

  9. 点菜网---Java开源生鲜电商平台-系统架构图(源码可下载)

    点菜网---Java开源生鲜电商平台-系统架构图(源码可下载) 1.点菜网-生鲜电商平台的价值与定位. 生鲜电商平台是一家致力于打造全国餐饮行业智能化.便利化.平台化与透明化服务的创新型移动互联网平台 ...

  10. Android 即时通讯开发小结(二)

    <Android 即时通讯开发小结>基于IM Andriod 开发的各种常见问题,结合网易云信即时通讯技术的实践,对IM 开发做一个全面的总结. 相关推荐阅读:. Android 即时通讯 ...