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四种线程池的 ...
随机推荐
- curl -s 不输出统计信息
curl -s 不输出统计信息 学习了:https://blog.csdn.net/qinyushuang/article/details/44114583
- [RxJS] Use `lift` to Connect a `source` to a `subscriber` in RxJS
The lift method on each source hides away the internals of RxJS so you can simply connect a source t ...
- Python 离线等价类
离线等价类的概念见离线等价类 最近在清洗数据的时候涉及到要将相似度比较高的文件夹合并,特征比对得到是1:1的对,比如: (a,b),(c,d),(a,c)...,那么合并的时候就涉及到将这些等价的对合 ...
- 3.NetDh框架之缓存操作类和二次开发模式简单设计(附源码和示例代码)
前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...
- sublime text 3 乱码
sublime text 是一款很好用的文字编辑软件,可谓是程序员必备,但是最近发现在mac端使用的时候,中文乱码, 网上一些解决方案,抄袭严重,没有解决实际问题,所以记录下自己解决问题的过程. 1. ...
- 对ASP.NET MVC 的路由一点理解
这个东西,真搞不懂.看了网上的教程和文章,也不懂(也不清楚写那些文章的人自己是否真的懂).只好靠自己一顿乱摸索. 好比说,下面这个路由: //路由1 config.Routes.MapHttpRout ...
- JDBC访问数据库查询信息的步骤(硬编码格式)
1 Class.forName()加载驱动 2 DriverManager获取Connect连接 3 创建Statement执行SQL语句 4 返回ResultSet查询结果 5释放资源 packag ...
- Swift学习笔记十四:构造(Initialization)
类和结构体在实例创建时,必须为全部存储型属性设置合适的初始值. 存储型属性的值不能处于一个未知的状态. 你能够在构造器中为存储型属性赋初值,也能够在定义属性时为其设置默认值.下面章节 ...
- 设计模式-(9)中介者模式(swift)
在对象去耦合的模式中,有两种模式:中介者模式,观察者模式 一,概念 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 这个 ...
- ubuntu LNMP环境下安装Redis,以及php的redis扩展
1.下载 sudo wget http://download.redis.io/releases/redis-4.0.9.tar.gz 2.解压 sudo tar zvxf redis-4.0.9.t ...