HandlerThread源码分析

概要介绍

HandlerThread是一种特殊的Thread,也就是有Looper的thread,既然有Looper的话,那我们就可以用此Looper来创建一个Handler,从而实现和它的交互。比如你可以通过与它关联的Handler对象在UI线程中发消息给它处理。HandlerThread一般可以用来执行某些background的操作,比如读写文件、进行一些计算(在此HandlerThread而非UI线程中)。既然还是一个Thread,那么和一般的Thread一样,也要通过调用其start()方法来启动它。它只是Android替我们封装的一个Helper类,其源码相当简洁,有一些经典的标准写法,我们一起来看看。

源码分析

和以往一样,我们先来看看其字段和构造函数:

    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; // 使用用户提供的优先级,取Process.java中定义的常量
    }

代码很简单,相关的分析都直接写在代码的注释里了,值得注意的是这里的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放自己的逻辑;其在looper开始前执行
    }

    @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。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,271评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,725评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,252评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,634评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,549评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,985评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,471评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,128评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,257评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,233评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,235评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,940评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,528评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,623评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,858评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,245评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,790评论 2 339

推荐阅读更多精彩内容