Android 线程池系列教程(2)Thread,Runnable是基类及如何写Run方法
1.This lesson teaches you to
2.You should also read
3.Try it out
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 moreRunnable
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
, andIntentService
. 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.
4.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
*/
...
}
...
}
5.Implement the run() Method
In the class, the Runnable.run()
method contains the code that's executed. Usually, anything is allowable in aRunnable
. 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 callingProcess.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 callingThread.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());
...
}
...
}
Android 线程池系列教程(2)Thread,Runnable是基类及如何写Run方法的更多相关文章
- Android 线程池系列教程(5)与UI线程通信要用Handler
Communicating with the UI Thread 上一课 下一课 1.This lesson teaches you to Define a Handler on the UI Thr ...
- Android 线程池系列教程(1)目录
Sending Operations to Multiple Threads 1.Dependencies and prerequisites Android 3.0 (API Level 11) o ...
- Android 线程池系列教程(4) 启动线程池中的线程和中止池中线程
Running Code on a Thread Pool Thread 上一课 下一课 1.This lesson teaches you to Run a Runnable on a Thre ...
- Android 线程池系列教程(3) 创建线程池
Creating a Manager for Multiple Threads 上一课 下一课 1.This lesson teaches you to Define the Thread Pool ...
- android线程池
线程池的基本思想还是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理.当有线程任务时,从池中取一个,执行完成后线程对象归池,这样可以避免反复创建线程 ...
- 线程池系列二:ThreadPoolExecutor讲解
一.简介 1)线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, i ...
- Android(java)学习笔记267:Android线程池形态
1. 线程池简介 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 创建线程时间, ...
- android线程池ThreadPoolExecutor的理解
android线程池ThreadPoolExecutor的理解 线程池 我自己理解看来.线程池顾名思义就是一个容器的意思,容纳的就是ThreadorRunable, 注意:每一个线程都是需要CPU分配 ...
- android 线程池的使用
转自http://www.trinea.cn/android/java-android-thread-pool/ Java(Android)线程池 介绍new Thread的弊端及Java四种线程池的 ...
随机推荐
- Scala入门到精通——第十六节 泛型与注解
本节主要内容 泛型(Generic Type)简单介绍 注解(Annotation)简单介绍 注解经常使用场景 1. 泛型(Generic Type)简单介绍 泛型用于指定方法或类能够接受随意类型參数 ...
- EJB学习(三)——java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to..
在上一篇博客介绍了怎样使用使用Eclipse+JBOSS创建第一个EJB项目,在这期间就遇到一个错误: Exception in thread "main" java.lang.C ...
- Angular2.x-主/细节组件
此刻,HeroesComponent显示heroes列表和所选heroes的详细信息. 随着应用程序的增长保持一个组件中的所有功能将不可维护.您需要将大型组件分成更小的子组件,每个组件都专注于特定的任 ...
- JavaSE入门学习23:Java面向对象之构造方法
学了JavaSE面向对象这一部分,也该对构造方法做一个总结了. 一构造方法 在多数情况下,初始化一个对象的终于步骤是去调用这个对象的构造方法. 构造方法负责对象的初始化工作,为 实例变量赋予合适的初始 ...
- 在类的头文件里尽量少引入其它头文件 <<Effective Objective-C>>
与C 和C++ 一样,Objective-C 也使用"头文件"(header file) 与"实现文件"(implementation file)来区隔代码.用 ...
- Java对象的创建过程
//TODO https://www.cnblogs.com/chenyangyao/p/5296807.html
- [IT练习册]Python练习项目 思路
1.爬虫:爬取如下网站一年的内容. http://www.calvarymoravian.org/dailytext 2.蛇形棋: 开发一个类似蛇形棋的游戏.最好基于Web. 3.爬虫+通讯录: 从公 ...
- Android调用本地WebService
package com.example.testinvokewebservice; import org.ksoap2.SoapEnvelope; import org.ksoap2.serializ ...
- HTML5你必须知道的28个新特性
1. 新的Doctype 尽管使用<!DOCTYPE html>,即使浏览器不懂这句话也会按照标准模式去渲染 2. Figure元素 用<figure>和<figcapt ...
- handsontable整理
hansontable简介 hansontable是一个在线类似Excel的表格编辑器,支持丰富的展现和交互,有多样的单元格类型供配置. 核心是由原生JavaScript构建,充分模块化,支持自定义b ...