• Android HandlerThread分析


    基础概念:
    HandlerThread 是一个包含 Looper 的 Thread,我们可以直接使用这个 Looper 创建 Handler,本质上还是Thread

    Handler 必须要和 Looper 中结合使用,尤其在子线程中创建 Handler 的话,需要这样写

    class LooperThread extends Thread {
        public Handler mHandler;
    
          public void run() {
              Looper.prepare();
    
              mHandler = new Handler() {
                  public void handleMessage(Message msg) {
                      // 这里处理消息
                  }
              };
    
              Looper.loop();
        }
    }

    可以看到,非常繁琐,一层套一层看着也不美观。HandlerThread 就是为了帮我们免去写上面那样的代码而生的。


    HandlerThread 的使用场景:
    我们知道,HandlerThread 所做的就是在新开的子线程中创建了 Looper,那它的使用场景就是 Thread + Looper 使用场景的结合,即:在子线程中执行耗时的、可能有多个任务的操作比如说多个网络请求操作,或者多文件 I/O 等等。


    源码分析:

    public class HandlerThread extends Thread {
        int mPriority;
        int mTid = -1;
        Looper mLooper;
    
        public HandlerThread(String name) {
            super(name);
            mPriority = Process.THREAD_PRIORITY_DEFAULT;
        }
        
        public HandlerThread(String name, int priority) {
            super(name);
            mPriority = priority;
        }
        
        protected void onLooperPrepared() {
        }
    
        @Override
        public 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;
        }
    
        public boolean quit() {
            Looper looper = getLooper();
            if (looper != null) {
                looper.quit();
                return true;
            }
            return false;
        }
    
    }

    1. HandlerThread 本质还是个 Thread,创建后别忘了调用 start()。
    2. 在 run() 方法中创建了 Looper,调用 onLooperPrepared 后开启了循环
    3. 我们要做的就是在子类中重写 onLooperPrepared,做一些初始化工作
    4. 在创建 HandlerThread 时可以指定优先级,注意这里的参数是 Process.XXX 而不是 Thread.XXX

    如果你够细心你会发现,run方法里面当mLooper创建完成后有个notifyAll(),getLooper()中有个wait(),这是为什么呢?因为的mLooper在一个线程中执行,而我们的handler是在UI线程初始化的,也就是说,我们必须等到mLooper创建完成,才能正确的返回getLooper();wait(),notify()就是为了解决这两个线程的同步问题。

    使用 HandlerThread 的典型例子就是 IntentService,


    参考:
    Android 进阶15:HandlerThread 使用场景及源码解析
    http://blog.csdn.net/u011240877/article/details/72905631

    Android HandlerThread 完全解析
    http://blog.csdn.net/lmj623565791/article/details/47079737

    Android HandlerThread 总结使用
    https://www.cnblogs.com/zhaoyanjun/p/6062880.html

  • 相关阅读:
    C#学习笔记一类型转换、枚举、foreach
    C#学习笔记四ref out参数
    SQL学习笔记一SQL基础
    C#学习笔记七索引器
    QUIC和TCP
    接口测试——测试点
    linux下 服务器资源监控工具nmon安装与使用
    Python执行.sh脚本cataline环境变量配置
    linux 常用命令之运行.sh文件
    jacoco+ant安装部署篇
  • 原文地址:https://www.cnblogs.com/lijunamneg/p/8550573.html
Copyright © 2020-2023  润新知