HandlerThread是一种特殊的Thread,也就是有Looper的thread,既然有looper的话,那我们就可以用此looper来

创建一个Handler,从而实现和它的交互,比如你可以通过与它关联的Handler对象在UI线程中发消息给它处理。HandlerThread

一般可以用来执行某些background的操作,比如读写文件(在此HandlerThread而非UI线程中)。既然还是一个Thread,那么

和一般的Thread一样,也要通过调用其start()方法来启动它。它只是Android替我们封装的一个Helper类,其源码相当简洁,我们

下面来看看,很简单。

  和以往一样,我们先来看看字段和ctor:

    int mPriority; // 线程优先级
int mTid = -1; // 线程id
Looper mLooper; // 与线程关联的Looper public HandlerThread(String name) { // 提供个名字,方便debug
super(name);
mPriority = Process.THREAD_PRIORITY_DEFAULT; // 没提供,则使用默认优先级
} /**
* Constructs a HandlerThread.
* @param name
* @param priority The priority to run the thread at. The value supplied must be from
* {@link android.os.Process} and not from java.lang.Thread.
*/
public HandlerThread(String name, int priority) {
super(name);
mPriority = priority; // 使用用户提供的优先级,基于linux优先级,取值在[-20,19]之间
}

代码很简单,相关的分析都直接写在代码的注释里了,值得注意的是这里的priority是基于linux的优先级的,而不是Java Thread

类里的MIN_PRIORITY,NORM_PRIORITY,MAX_PRIORITY之类,请注意区分(其实认真阅读方法的doc即可)。

  接下来看看此类的关键3个方法:

   /**
* Call back method that can be explicitly overridden if needed to execute some
* setup before Looper loops.
*/
protected void onLooperPrepared() { // callback方法,如果你愿意可以Override放自己的逻辑;其在loop开始前执行
} @Override
public void run() {
mTid = Process.myTid();
Looper.prepare(); // 此方法我们前面介绍过,会创建与线程关联的Looper对象
synchronized (this) { // 进入同步块,当mLooper变的可用的使用,调用notifyAll通知其他可能block在当前对象上的线程
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority); // 设置线程优先级
onLooperPrepared(); // 调用回调函数
Looper.loop(); // 开始loop
mTid = -1; // reset为invalid值
} /**
* This method returns the Looper associated with this thread. If this thread not been started
* or for any reason is isAlive() returns false, this method will return null. If this thread
* has been started, this method will block until the looper has been initialized.
* @return The looper.
*/
public Looper getLooper() {
if (!isAlive()) { // 如果线程不是在alive状态则直接返回null,有可能是你忘记调start方法了。。。
return null;
} // If the thread has been started, wait until the looper has been created.
synchronized (this) {
while (isAlive() && mLooper == null) { // 进入同步块,当条件不满足时无限等待,
try { // 直到mLooper被设置成有效值了才退出while(当然也可能是线程状态不满足);
wait(); // run方法里的notifyAll就是用来唤醒这里的
} catch (InterruptedException e) { // 忽略InterruptedException
}
}
}
return mLooper; // 最后返回mLooper,此时可以保证是有效值了。
}

当你new一个HandlerThread的对象时记得调用其start()方法,然后你可以接着调用其getLooper()方法来new一个Handler对象,

最后你就可以利用此Handler对象来往HandlerThread发送消息来让它为你干活了。

  最后来看2个退出HandlerThread的方法,其实对应的是Looper的2个退出方法:

    /**
* Quits the handler thread's looper.
* <p>
* Causes the handler thread's looper to terminate without processing any
* more messages in the message queue.
* </p><p>
* Any attempt to post messages to the queue after the looper is asked to quit will fail.
* For example, the {@link Handler#sendMessage(Message)} method will return false.
* </p><p class="note">
* Using this method may be unsafe because some messages may not be delivered
* before the looper terminates. Consider using {@link #quitSafely} instead to ensure
* that all pending work is completed in an orderly manner.
* </p>
*
* @return True if the looper looper has been asked to quit or false if the
* thread had not yet started running.
*
* @see #quitSafely
*/
public boolean quit() {
Looper looper = getLooper(); // 注意这里是调用getLooper而不是直接使用mLooper,
if (looper != null) { // 因为mLooper可能还没初始化完成,而调用方法可以
looper.quit(); // 等待初始化完成。
return true;
}
return false;
} /**
* Quits the handler thread's looper safely.
* <p>
* Causes the handler thread's looper to terminate as soon as all remaining messages
* in the message queue that are already due to be delivered have been handled.
* Pending delayed messages with due times in the future will not be delivered.
* </p><p>
* Any attempt to post messages to the queue after the looper is asked to quit will fail.
* For example, the {@link Handler#sendMessage(Message)} method will return false.
* </p><p>
* If the thread has not been started or has finished (that is if
* {@link #getLooper} returns null), then false is returned.
* Otherwise the looper is asked to quit and true is returned.
* </p>
*
* @return True if the looper looper has been asked to quit or false if the
* thread had not yet started running.
*/
public boolean quitSafely() {
Looper looper = getLooper();
if (looper != null) {
looper.quitSafely();
return true;
}
return false;
}

通过代码我们可以看到其内部都是delegate给了Looper对象,而Looper我们在前面也介绍过了,感兴趣的同学可以翻看前面的分析或者

查看这2个方法的doc,写的都很详细。

  至此这个简单的Handy class就算分析完毕了。在实际的开发中,如果你只是要做某些后台的操作(短暂的,比如把某些设置文件load

到内存中),而不需要更新UI的话,那你可以优先使用HandlerThread而不是AsyncTask。

  接下来准备分析下Android提供的数据存储机制SharedPreferences,敬请期待。。。

Android源码分析之HandlerThread的更多相关文章

  1. Android源码分析-全面理解Context

    前言 Context在android中的作用不言而喻,当我们访问当前应用的资源,启动一个新的activity的时候都需要提供Context,而这个Context到底是什么呢,这个问题好像很好回答又好像 ...

  2. Android源码分析(六)-----蓝牙Bluetooth源码目录分析

    一 :Bluetooth 的设置应用 packages\apps\Settings\src\com\android\settings\bluetooth* 蓝牙设置应用及设置参数,蓝牙状态,蓝牙设备等 ...

  3. Android源码分析(十七)----init.rc文件添加脚本代码

    一:init.rc文件修改 开机后运行一次: chmod 777 /system/bin/bt_config.sh service bt_config /system/bin/bt_config.sh ...

  4. Android源码分析(十六)----adb shell 命令进行OTA升级

    一: 进入shell命令界面 adb shell 二:创建目录/cache/recovery mkdir /cache/recovery 如果系统中已有此目录,则会提示已存在. 三: 修改文件夹权限 ...

  5. Android源码分析(十五)----GPS冷启动实现原理分析

    一:原理分析 主要sendExtraCommand方法中传递两个参数, 根据如下源码可以知道第一个参数传递delete_aiding_data,第二个参数传递null即可. @Override pub ...

  6. Android源码分析(十四)----如何使用SharedPreferencce保存数据

    一:SharedPreference如何使用 此文章只是提供一种数据保存的方式, 具体使用场景请根据需求情况自行调整. EditText添加saveData点击事件, 保存数据. diff --git ...

  7. Android源码分析(十三)----SystemUI下拉状态栏如何添加快捷开关

    一:如何添加快捷开关 源码路径:frameworks/base/packages/SystemUI/res/values/config.xml 添加headset快捷开关,参考如下修改. Index: ...

  8. Android源码分析(十二)-----Android源码中如何自定义TextView实现滚动效果

    一:如何自定义TextView实现滚动效果 继承TextView基类 重写构造方法 修改isFocused()方法,获取焦点. /* * Copyright (C) 2015 The Android ...

  9. Android源码分析(十一)-----Android源码中如何引用aar文件

    一:aar文件如何引用 系统Settings中引用bidehelper-1.1.12.aar 文件为例 源码地址:packages/apps/Settings/Android.mk LOCAL_PAT ...

随机推荐

  1. DDD:整理了一些关于验证方面的文章

    http://msdn.microsoft.com/en-us/library/ff664356(v=pandp.50).aspx http://gorodinski.com/blog/2012/05 ...

  2. 调用Ajax返回500错误的解决方法

    看代码: public ActionResult UserLogin(LogOnModel model) { #region 验证码验证 #endregion OperationResult resu ...

  3. 二叉查找树(二)之 C++的实现

    概要 上一章介绍了"二叉查找树的相关理论知识,并通过C语言实现了二叉查找树".这一章给出二叉查找树的C++版本.这里不再对树的相关概念进行介绍,若遇到不明白的概念,可以在上一章查找 ...

  4. sql order by 排序多个字段

    order by 多个字段,每个字段后面都有排序方式,默认ASC 例如:select table a order by a.time1 ,a.time2 desc,a.time3 asc

  5. 使用 Responsive Elements 快速构建响应式网站

    Responsive Elements 可以使任何元素来适应和应对他们所占据的区域.这是一个轻量的 JavaScript 库,你可以轻松嵌入到你的项目.元素会更具自己的宽度,自动响应和适应空间的增加或 ...

  6. Android学习笔记之SoftReference软引用...

    PS:其实这一篇和上一篇很类似,都是为了解决内存不足(OOM)这种情况的发生... 学习内容: 1.对象的引用类....   最近也是通过项目中知道了一些东西,涉及到了对象的引用类,对象的引用类分为多 ...

  7. R提高篇(二): 图形初阶

    目录: 图形示例 图形参数 符号.线条 颜色 文本属性 尺寸与边界 自定义标题 自定义坐标轴 图例 文本标注 图形组合 图形示例 如下代码描述病人对两种药物五个剂量水平上的响应情况 > myda ...

  8. java.lang.IllegalArgumentException 不合法的参数异常

    报错内容: IllegalArgumentException 不合法的参数异常 十二月 06, 2016 10:06:56 上午 org.apache.catalina.core.StandardWr ...

  9. jquery.ajax 跨域请求webapi,设置headers

    解决跨域调用服务并设置headers 主要的解决方法需要通过服务器端设置响应头.正确响应options请求,正确设置 JavaScript端需要设置的headers信息 方能实现. 1.第一步 服务端 ...

  10. Diy页面服务端渲染解决方案

    1. 问题由来 在移动互联网电商领域,运营每天需要搭建多个促销页面来吸引用户去点击去购买,一开始程序员临时写个新页面去实现,可这些页面可以用几次就不用了,每次创建新页面去实现费时费力,而且,电商的运营 ...