美文网首页
Handler 原理

Handler 原理

作者: 马路牙子666 | 来源:发表于2021-05-18 10:36 被阅读0次

    1.为什么要用Handler

    在Android中主线程又称为UI线程,负责创建和更新UI,其他耗时操作如,访问数据库,网络请求,操作文件IO都需要放在子线程中执行。
    Android屏幕刷新大概是1秒60帧,每帧间隔16.67毫秒,为了保证刷新不掉帧,所以将耗时操作全部放在子线程中处理。
    当子线程处理完数据之后,为了防止UI处理逻辑的混乱,Android只允许主线程处理UI,这时候需要Handler来充当子线程和主线程之间的桥梁。

    为什么不加锁:

    1.锁会让UI逻辑混乱,无法保证执行顺序。
    2.锁会降低UI更新效率,会阻塞某些线程的执行。
    3.Android利用Handler隐示的添加了一把只能在主线程更新的锁。因此Handler的消息处理机制是线程安全的
    ps:在某些情况下可以在子线程更新UI,例如onCreate ,主线程更新完UI后马上子线程再次更新UI ,或者在子线程更新固定宽高的控件。

    2.Handler消息机制的原理

    本质就是Handler发送Message到MessageQueue中,Looper遍历消息分发给Hanlder处理。

    Handler 主要涉及四个类
    1.Message:消息
    每个消息的携带者持有Handler,存放在MessageQueue中,一个线程可以多个实例。
    2.Hanlder:消息的发起者,处理者
    发送Message及处理回调事件,一个线程可以存在多个实例。
    3.Looper:消息的遍历者
    从MessageQueue中遍历出每个Message进行处理,一个线程一个实例。
    4.MessageQueue:消息队列
    存放Handler发送的消息,提供给Looper遍历,一个线程一个。

    Looper.prepare()

    方法中在ThreadLocal中获取一个Looper。
    因此确保每个线程中只有一个Looper。

        public static void prepareMainLooper() {
            prepare(false);
            synchronized (Looper.class) {
                if (sMainLooper != null) {
                    throw new IllegalStateException("The main Looper has already been prepared.");
                }
                sMainLooper = myLooper();
            }
        }
    
    
        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));
        }
    

    3.Looper的启动

    ActivityThread作为程序的第一个入口,main方法中调用Looper.prepareMainLooper创建了一个Looper。
    并调用Looper.loop();方法

        public static void main(String[] args) {
            ...
            Looper.prepareMainLooper();
    
            ...
            ActivityThread thread = new ActivityThread();
            thread.attach(false, startSeq);
    
            if (sMainThreadHandler == null) {
                sMainThreadHandler = thread.getHandler();
            }
    
            if (false) {
                Looper.myLooper().setMessageLogging(new
                        LogPrinter(Log.DEBUG, "ActivityThread"));
            }
    
            // End of event ActivityThreadMain.
            Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
            Looper.loop();
    
            throw new RuntimeException("Main thread loop unexpectedly exited");
        }
    
    

    4.Looper的循环

        public static void loop() {
            //获取当前线程的Looper对象
            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;
            ...
            //死循环
            for (;;) {
                //在消息队列中获取消息
                Message msg = queue.next(); // might block
                if (msg == null) {
                    return;
                }
               ...
                try {
                    //target 是 Message 中保存处理消息的Handler实例。
                    msg.target.dispatchMessage(msg);
                } catch (Exception exception) {
                    throw exception;
                }
                ...
                msg.recycleUnchecked();
            }
        }
    

    5.MessageQueue的next();

    如果没有消息可以回去会执行nativePollOnce方法陷入沉睡, 等待唤醒。
    添加消息到队列中enqueueMessage方法会进行调用nativeWake方法唤醒。

    6.nativePollOnce 和 nativeWake

    目前还无法看懂具体实现。后续看明白会补上。

        private native void nativePollOnce(long ptr, int timeoutMillis); /*non-static for callbacks*/
        private native static void nativeWake(long ptr);
    

    相关文章

      网友评论

          本文标题:Handler 原理

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