Specifying the Code to Run on a Thread

1.This lesson teaches you to

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

2.You should also read

3.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 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 HandlerThreadAsyncTask, andIntentServiceThread 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方法的更多相关文章

  1. Android 线程池系列教程(5)与UI线程通信要用Handler

    Communicating with the UI Thread 上一课 下一课 1.This lesson teaches you to Define a Handler on the UI Thr ...

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

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

  3. Android 线程池系列教程(4) 启动线程池中的线程和中止池中线程

    Running Code on a Thread Pool Thread 上一课   下一课 1.This lesson teaches you to Run a Runnable on a Thre ...

  4. Android 线程池系列教程(3) 创建线程池

    Creating a Manager for Multiple Threads 上一课  下一课 1.This lesson teaches you to Define the Thread Pool ...

  5. android线程池

    线程池的基本思想还是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理.当有线程任务时,从池中取一个,执行完成后线程对象归池,这样可以避免反复创建线程 ...

  6. 线程池系列二:ThreadPoolExecutor讲解

    一.简介 1)线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, i ...

  7. Android(java)学习笔记267:Android线程池形态

    1. 线程池简介  多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力.     假设一个服务器完成一项任务所需时间为:T1 创建线程时间, ...

  8. android线程池ThreadPoolExecutor的理解

    android线程池ThreadPoolExecutor的理解 线程池 我自己理解看来.线程池顾名思义就是一个容器的意思,容纳的就是ThreadorRunable, 注意:每一个线程都是需要CPU分配 ...

  9. android 线程池的使用

    转自http://www.trinea.cn/android/java-android-thread-pool/ Java(Android)线程池 介绍new Thread的弊端及Java四种线程池的 ...

随机推荐

  1. 开源yYmVc项目,邀您和我一起开发:)

    打算在闲暇时间写个MVC框架,要有什么功能一步一步边写边加,仿照struts 2 和 spring mvc.假设您感兴趣的话,能够私密我,给您加入key:). 欢迎您的到来~ 项目放在基于GIT的CS ...

  2. 安装PyQt5和Eric6

    安装官方的指引,安装起来本来是非常简单的,但是我前后折腾了两天,甚至连Eric得源码都去调试都没成功.过程如下: 在PyQt5的官网链接中下载轮子 PyQt5-5.7.1-5.7.1-cp34.cp3 ...

  3. linux安装anaconda中的问题及解决办法

    安装过程: 0:在ananconda官网网站上下载anaconda的linux版本https://www.anaconda.com/download/: 1:linux上切换到下载目录后(用cd), ...

  4. Flex+Java+Blazeds

    1.环境:jdk1.6,Flex4.6 2.工具:MyEclipse10 3.server:Tomcat7 4.连接方式:Blazeds 5.项目类型:Flex项目 6.步骤 (1)新建Flex项目一 ...

  5. ISA总线

    ISA总线: (Industry Standard Architecture:工业标准体系结构)是为PC/AT电脑而制定的总线标准,为16位体系结构,只能支持16位的I/O设备,数据传输率大约是16M ...

  6. 关于追踪qemu 源码函数路径的一个方法

    这阵子一直在研究qemu 磁盘io路径的源码,发现直接看代码是意见非常低效率的事情,qemu是一个比较庞大的家伙(源码部分大概154MB,完全由C语言来完成),整个结构也都非常地复杂,所以从代码上研究 ...

  7. [TJOI2016&HEOI2016] 排序

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4552 [算法] 首先 , 二分答案x , 将比x小的数看作1,比x大的数看作0 然后 ...

  8. RDA GUI

    创建服务:“sysapp_table.h” / "dvb_guiobj_table.h" / "atv_guiobj_table.h" “服务”与“回调”   ...

  9. 小程序-demo:template

    ylbtech-小程序-demo: 1.返回顶部 1.app.js 2.app.json 3.app.wxss 4.project.config.json 5.pages 6.images 7. 2. ...

  10. Scanner类nextLine()和next()的区别和使用方法

    next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键.Tab键或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的 ...