美文网首页android线程相关
Handler消息传递机制

Handler消息传递机制

作者: 真胖大海 | 来源:发表于2017-11-19 22:44 被阅读17次

demo

一.Handler消息传递机制解决的问题

线程通信

耗时工作(从服务器拉去数据) 更新UI
子线程(避免方式ANR,就是主线程卡死) 主线程

要想在子线程做完耗时工作后及时更新UI,就必须使用Handler消息传递机制。

二. Handler工作流程

  1. Handler.post/sendMessage 方法最终都会调用enqueueMessage将消息放入消息队列


    image

    2.在子线程中放入消息,在主线程中取出消息


    link

三. 源码分析

enqueueMessage

将消息插入到消息队列的正确位置

    msg.markInUse();
            msg.when = when;
            Message p = mMessages;
            boolean needWake;
            if (p == null || when == 0 || when < p.when) {
                // New head, wake up the event queue if blocked.
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked;
            } else {
                // Inserted within the middle of the queue.  Usually we don't have to wake
                // up the event queue unless there is a barrier at the head of the queue
                // and the message is the earliest asynchronous message in the queue.
                needWake = mBlocked && p.target == null && msg.isAsynchronous();
                Message prev;
                for (;;) {
                    prev = p;
                    p = p.next;
                    if (p == null || when < p.when) {
                        break;
                    }
                    if (needWake && p.isAsynchronous()) {
                        needWake = false;
                    }
                }
                msg.next = p; // invariant: p == prev.next
                prev.next = msg;
            }

Looper

queue.next();从消息对列中取出消息,如果没有消息就阻塞(主线程进入休眠)。
msg.target就是Handler
msg.target.dispatchMessage(msg)调用Handler的dispatchMessage处理消息
Looper的参考

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

            try {
                msg.target.dispatchMessage(msg);
            }
        }
    }

Handler

处理消息
mCallback是Handler带参构造函数new Handler(new Handler.Callback())传入的。

    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
    }
    
       private static void handleCallback(Message message) {
        message.callback.run();
    }

相关文章

  • Android Handler消息传递机制

    一、Handler消息传递机制简介 1.什么是Handler Handler是Android的一套消息传递机制。在...

  • 浅析Handler消息传递机制

    Android的异步消息处理机制:Handler消息传递机制。 1、Message Message是在线程之间传递...

  • Handler消息传递机制

    一、Handler消息传递机制和深入认识(在一个线程中,可以有多个Handler?): (一)、引入: 子线程没有...

  • Handler消息传递机制

    Android为了线程安全,并不允许我们在UI线程外操作UI;很多时候我们做界面刷新都需要通过Handler来通知...

  • Handler消息传递机制

    前言 Handler 的消息传递机制是我们面试中常被问到的问题,它到底如何去实现的呢?这样做的好处是什么呢?接下来...

  • Handler消息传递机制

    demo 一.Handler消息传递机制解决的问题 线程通信 要想在子线程做完耗时工作后及时更新UI,就必须使用H...

  • Handler消息传递机制

    Handler消息传递机制——源码赏析 Android的消息处理有四个核心类:Handler、Looper、Mes...

  • Android消息传递机制

    Android消息传递机制 一、概述 Android消息机制主要是指 Handler 的运行机制以及 Handle...

  • Android中UI的更新方式

    使用Handler消息传递机制; 使用AsyncTask异步任务; 使用runOnUiThread(action)...

  • Handler机制详解

    1. Handler简介 Handler机制,说的也是Android中的消息传递机制。也就是将工作线程(子线程)中...

网友评论

    本文标题:Handler消息传递机制

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