深入理解HandlerThread

深入理解HandlerThread以往遇到HandlerThread,对它的认识只是停留在MessageLooperHandler上,知道它有自己的消息队列,仅此而已。随着编程的深入,个人已不再满足表面上的理解,所以再次翻开HandlerThread源码,做梳理记录。HandlerThread集成Thread,并重写了Thread类的run方法(如果我们自定义一个类继承HandlerThread,就用不到run函数了):

大家好,又见面了,我是你们的朋友全栈君。

以往遇到HandlerThread,对它的认识只是停留在Message<->Looper<->Handler上,知道它有自己的消息队列,仅此而已。随着编程的深入,表面上的理解无法满足要求,所以再次翻开HandlerThread源码,做梳理记录。

HandlerThread集成Thread,并重写了Thread类的run方法(如果我们自定义一个类继承HandlerThread,就用不到run函数了):

    @Override
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        mTid = -1;
    }

从图中我们可以看到,HandlerThread的run方法先后执行了Looper.prepare()、Looper.loop(),因为我们知道这样做的目的是为了给HandlerThread实例建立一个属于它的消息队列(MessageQueue,这一点像极了Windows系统的窗口消息分发机制)。

     /** Initialize the current thread as a looper.
      * This gives you a chance to create handlers that then reference
      * this looper, before actually starting the loop. Be sure to call
      * {@link #loop()} after calling this method, and end it by calling
      * {@link #quit()}.
      */
    public static void prepare() {
        prepare(true);
    }

    private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper(quitAllowed));
    }

Looper.loop()的工作就是开始不断的从消息队列中取出消息(在loop函数里有个for循环,除非线程被系统杀死或者被调用quit函数,负责for循环会一直执行)

    /**
     * Run the message queue in this thread. Be sure to call
     * {@link #quit()} to end the loop.
     */
    public static void loop() {
        final Looper me = myLooper();
        if (me == null) {
            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
        }
        final MessageQueue queue = me.mQueue;

        // Make sure the identity of this thread is that of the local process,
        // and keep track of what that identity token actually is.
        Binder.clearCallingIdentity();
        final long ident = Binder.clearCallingIdentity();

        for (;;) {
            Message msg = queue.next(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }

            // This must be in a local variable, in case a UI event sets the logger
            Printer logging = me.mLogging;
            if (logging != null) {
                logging.println(">>>>> Dispatching to " + msg.target + " " +
                        msg.callback + ": " + msg.what);
            }

            msg.target.dispatchMessage(msg);

            if (logging != null) {
                logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
            }

            // Make sure that during the course of dispatching the
            // identity of the thread wasn't corrupted.
            final long newIdent = Binder.clearCallingIdentity();
            if (ident != newIdent) {
                Log.wtf(TAG, "Thread identity changed from 0x"
                        + Long.toHexString(ident) + " to 0x"
                        + Long.toHexString(newIdent) + " while dispatching to "
                        + msg.target.getClass().getName() + " "
                        + msg.callback + " what=" + msg.what);
            }

            msg.recycleUnchecked();
        }
    }

有人说Looper中的sThreadLocal属性(ThreadLocal<Looper>类型)是个静态常量,怎么保证每个执行过Looper.prepare()、Looper.loop()的线程都有唯一的Looper呢?这个问题刚开始我也很好奇,后来发现原因在ThreadLocal类。

/**
 * Implements a thread-local storage, that is, a variable for which each thread
 * has its own value. All threads share the same {@code ThreadLocal} object,
 * but each sees a different value when accessing it, and changes made by one
 * thread do not affect the other threads. The implementation supports
 * {@code null} values.
 *
 * @see java.lang.Thread
 * @author Bob Lee
 */
public class ThreadLocal<T> 

上述类的解释是说ThreadLocal实现了线程的本地存储,即所有的线程共同使用同一个ThreadLocal对象,但每个线程都会有一个ThreadLocal对象副本,每个线程的ThreadLocal对象副本又是相互独立的,互不影响。

推荐一篇介绍ThreadLocal的博文,写的很不错:彻底理解ThreadLocal

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157929.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • window下phpstudy的nginx配置虚拟主机

    window下phpstudy的nginx配置虚拟主机

    2021年10月15日
    52
  • js 数组插入删除[通俗易懂]

    js 数组插入删除[通俗易懂]常用的方法是遍历数组,然后使用splice()删除这里我们使用es6中findIndex()查找,然后删除functiondeleteFromArray(arr,compare){constindex=arr.findIndex(compare)if(index>-1){arr.splice(index,1)}}插入数据functioninsertArray(arr,val,compare,maxLen){//返回位置consti

    2022年9月30日
    0
  • js对日期进行升序排序

    js对日期进行升序排序

    2021年11月22日
    52
  • 现代语音信号处理笔记 (一)

    现代语音信号处理笔记 (一)本系列笔记对胡航老师的现代语音信号处理这本书的语音处理部分进行总结,包含语音信号处理基础、语音信号分析、语音编码三部分。一开始以为三部分总结到一篇文章里就可以了,但写着写着发现事情并没有那么简单。。。因此还是老老实实的总结吧,扎实的基础最重要。语音信号处理基础语音信号的处理简称语音处理,是用数字信号处理技术对语音信号进行处理的一门学科。语音信号均采用数字方式进行处理,语音信号的数字…

    2022年5月26日
    40
  • 乐观锁和悲观锁实现(java乐观锁实现)

    推荐阅读:如何在技术领域持续成长后端程序员必备的Linux基础知识后端必备——数据通信知识(RPC、消息队列)一站式总结何谓悲观锁与乐观锁乐观锁对应于生活中乐观的人总是想着事情往好的方向发展,悲观锁对应于生活中悲观的人总是想着事情往坏的方向发展。这两种人各有优缺点,不能不以场景而定说一种人好于另外一种人。悲观锁总是假设最坏的情况,每次去拿数据的时候都认为…

    2022年4月18日
    46
  • 备份集中的数据库备份与现有的数据库不同解决方法

    备份集中的数据库备份与现有的数据库不同解决方法以前一直使用SQLServer2000,现在跟潮流都这么紧,而且制定要求使用SQLServer2005,就在现在的项目中使用它了。对于SQLServer2005,有几个地方是要注意的,比方在还原数据库时,不像2000里边将数据库和文件区分的很细,统一均为文件,这就使还原的数据库文件制定为.bak。那么想还原2000的数据库(备份数据库文件,无后缀名的),就需要自己手工选择。…

    2022年5月6日
    441

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号