HandlerThread源码解析

作者: 未见哥哥 | 来源:发表于2017-06-19 14:53 被阅读77次

1、HandlerThread 是什么?

它继承至 Thread,具备线程的特性。它是一个带有 Looper 的线程,并且该Looper可以被用于创建对应的Handler。

2、HandlerThread的作用是什么?

当一个线程执行完一个耗时任务之后,如果想要再执行另外一个耗时任务那么就需要重新创建一个新的线程,而多次进行线程的开启和销毁的是需要很大的开销。在一般情况下,线程被开启之后,也就是run方法会被执行,若需要动态地在执行过程做一些其它处理,例如根据不同的条件执行不同的代码块,显然是比较麻烦的。而HandlerThread解决了这个问题,它内部维护了一个Looper不断地去轮训消息队列,当需要在子线程做其他操作时,只需要通过绑定的Handler去发送消息,然后处理即可。

3、HandlerThread的工作原理

HandlerThread 是一个内部维护一个 Looper 对象的线程。这样就省去了创建线程时需要手动的创建 Looper 的麻烦。而已它内部使用了
wait() 和 notifyAll() 保证的 Looper 的非空性控制。

3.1、HandlerThread#run()

  • 该方法是一个loop()的死循环,等待消息的到来。
  • Looper.prepare()创建Looer对象并保存到ThreadLocal中。
  • Looper.myLooper()获取通过preparen()保存的Looper对象。
  • loop()开启轮训器轮训。
public void run() {
    mTid = Process.myTid();
    Looper.prepare();
    synchronized (this) {
        
        mLooper = Looper.myLooper();
        //在这里的 Looper 已经创建完毕,通知 getLooper 不需要等待啦。
        notifyAll();
    }
    Process.setThreadPriority(mPriority);
    onLooperPrepared();//空的回调,交由用户做一些loop()前的准备工作。
    Looper.loop();
    mTid = -1;
}

3.2、HandlerThread#getLooper()

获取关联的Looper对象,可用于外部创建Handler的构造参数,该方法中有一个wait()方法,然后在run()中一个notifyAll()方法,他们的作用就是在getLooper时为了保证能正确的获取到当looper对象,当looper为null,就让其等待,直到run()方法创建了其Looper对象之后调用notifyAll唤醒。

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;
}

3.3、移除消息队列中的消息

//立即退出
public boolean quit() {
    Looper looper = getLooper();
    if (looper != null) {
        looper.quit();
        return true;
    }
    return false;
}

//安全的退出
public boolean quitSafely() {
    Looper looper = getLooper();
    if (looper != null) {
        looper.quitSafely();
        return true;
    }
    return false;
}

4、 IntentService 内部 HandlerThread 的应用


@Override
public void onCreate() {
    super.onCreate();
    HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
    //开启线程
    thread.start();
    //getLooper 是一个阻塞方法。
    mServiceLooper = thread.getLooper();
    //Handler 绑定一个通过 HandlerThread 创建的 looper 对象。
    mServiceHandler = new ServiceHandler(mServiceLooper);
}

相关文章

  • 3.3异步消息处理机制-HandlerThread

    HandlerThread handlerThread是什么 handlerThread源码解析 1.handle...

  • 10 异步3-HandlerThread

    1)handlerThread是什么2)handlerThread源码解析 1、handlerThread产生背景...

  • HandlerThread 源码解析

    在日常的开发中经常需要使用到Handler这个类,我们都知道Handler 必须要和 Looper 中结合使用,尤...

  • HandlerThread源码解析

    HandlerThread源码 它是一个Thread的子类,源码很简单,只有100多行代码1、 run() 可以看...

  • HandlerThread 源码解析

    HandlerThread 是什么? 系统对 HandlerThread 的解释是这么一段话: 从上面这段话可以看...

  • HandlerThread源码解析

    上一篇文章对HandlerThread的概念及用法做了一个基本的介绍,并且使用HandlerThread改写了相册...

  • HandlerThread源码解析

    1、HandlerThread 是什么? 它继承至 Thread,具备线程的特性。它是一个带有 Looper 的线...

  • HandlerThread源码解析

    概述 HandlerThread是Thread的子类,由HandlerThread创建的子线程可以直接创建Hand...

  • HandlerThread源码解析

    我对HandlerThread的理解是实现了一套消息通信机制的Thread。和它有关的两个重要概念是Looper(...

  • HandlerThread源码解析

    其实从HandlerThread这个名字大家应该也能猜得出来,这是Handler和线程相关的类,如果你理解了Han...

网友评论

    本文标题:HandlerThread源码解析

    本文链接:https://www.haomeiwen.com/subject/cnigqxtx.html