博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为什么要用HandlerThread,怎么用?
阅读量:2072 次
发布时间:2019-04-29

本文共 2686 字,大约阅读时间需要 8 分钟。

HandlerThread是Thread的子类,它的作用很明确,文档说的也很清楚

Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.

意思就是说HandlerThread帮我们创建好了Looper。

我们都知道,Android主线程的Looper是自动创建的,其他线程是没有创建Looper的,需要我们自己创建。一般做法很简单

@Override public void run() {    Looper.prepare();    Looper.loop();}

prepare()loop()两个方法不再赘述,我们先来看一个不用HandlerThread的例子:

Thread newThread = new Thread(new Runnable()  {        @Override        public void run() {              Looper.prepare();              Looper.loop();        }  });newThread.start();Handler handler = new Handler(newThread.getLooper());

相信不少人会用上面的方式创建一个异步线程的Handler,有没有问题呢?肯定有。

newThread的looper是在这个线程运行之后创建的,所以,当执行到Handler handler = new Handler(newThread.getLooper());的时候,newThread的looper可能还没有创建好!
****这就是为什么我们需要HandlerThread,并不仅仅是因为它帮我们创建了一个looper,更重要的是它为我们处理了这个异步问题。****
来看下HandlerThread的实现:

@Overridepublic void run() {      mTid = Process.myTid();      Looper.prepare();      synchronized (this) {            mLooper = Looper.myLooper();            notifyAll();      }      Process.setThreadPriority(mPriority);      onLooperPrepared();      Looper.loop();      mTid = -1;}public Looper getLooper() {      if (!isAlive()) {            return null;      }          // If the thread has been started, wait until the looper has been created.      synchronized (this) {            while (isAlive() && mLooper == null) {                  try {                        wait();                  }       catch (InterruptedException e) { }            }      }      return mLooper;}

简单的一个加锁帮我们做了最重要的事情。

有人问我在run()方法里面,mTid开始赋值Process.myTid(),为什么后来又复制-1了呢?仔细想一下就有答案了,因为Looper.loop()是个死循环啊,执行到mTid = -1的时候,就是looper退出的时候。


插一句,这个mTid是干嘛的?

/** * Returns the identifier of this thread. See Process.myTid(). */public int getThreadId() {      return mTid;}

Thread里面是没有getThreadId()方法的,Process.myTid()方法定义如下:

/** * Returns the identifier of the calling thread, which be used with * {@link #setThreadPriority(int, int)}. */public static final int myTid() {      return Libcore.os.gettid();}

原来tid是修改线程优先级、调度策略时用来做线程唯一标识的。那么在HandleThread中,把mTid置为-1是几个意思?笔者的理解是,HandlerThread本身是为looper服务的,looper终止以后,线程也会马上终止,为防止开发者错误使用,所以将mTid置为-1。


关于HandlerThread另外一个容易忽略的问题就是退出Looper。Looper通过quit()quitSafely()方法退出(记得,Looper.loop()之后是一个死循环),看看Looper的loop()方法的注释

/** * Run the message queue in this thread. Be sure to call * {@link #quit()} to end the loop. */public static void loop()

Looper使用完毕执行quit本身就是一件容易忽略的事情,如果放到HandlerThread中,更是容易忘得一干二净。所以HandlerThread为我们提供了两个相同的方法:

public boolean quit(){}public boolean quitSafely(){}

****不过说到底,维护Looper的生命周期还是我们每个开发者自己的事情,HandlerThread只不过是封装了一下,帮我们处理一个异步问题罢了。****

转载地址:http://ktvmf.baihongyu.com/

你可能感兴趣的文章
FastDFS集群架构配置搭建(转载)
查看>>
HTM+CSS实现立方体图片旋转展示效果
查看>>
FFmpeg 命令操作音视频
查看>>
问题:Opencv(3.1.0/3.4)找不到 /opencv2/gpu/gpu.hpp 问题
查看>>
目的:使用CUDA环境变量CUDA_VISIBLE_DEVICES来限定CUDA程序所能使用的GPU设备
查看>>
问题:Mysql中字段类型为text的值, java使用selectByExample查询为null
查看>>
程序员--学习之路--技巧
查看>>
解决问题之 MySQL慢查询日志设置
查看>>
contOS6 部署 lnmp、FTP、composer、ThinkPHP5、docker详细步骤
查看>>
TP5.1模板布局中遇到的坑,配置完不生效解决办法
查看>>
PHPstudy中遇到的坑No input file specified,以及传到linux环境下遇到的坑,模板文件不存在
查看>>
TP5.1事务操作和TP5事务回滚操作多表
查看>>
composer install或composer update 或 composer require phpoffice/phpexcel 失败解决办法
查看>>
TP5.1项目从windows的Apache服务迁移到linux的Nginx服务需要注意几点。
查看>>
win10安装软件 打开时报错 找不到 msvcp120.dll
查看>>
PHPunit+Xdebug代码覆盖率以及遇到的问题汇总
查看>>
PHPUnit安装及使用
查看>>
PHP项目用xhprof性能分析(安装及应用实例)
查看>>
composer安装YII
查看>>
Sublime text3快捷键演示
查看>>