Android 源码分析

Android Hander - 消息循环机制

2019-02-27  本文已影响0人  一盏旧旧的灯
一、 背景介绍
private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
    }
};

mHandler.sendMessage(new Message());
二、简要流程图
三、Handler - 发送消息
public final boolean sendMessage(Message msg) {
    //最简单的发送方法 - 内部调用的其他方法。
    return sendMessageDelayed(msg, 0);
}

public final boolean sendEmptyMessage(int what) {
    return sendEmptyMessageDelayed(what, 0);
}

/**
 * 延迟delayMillis 毫秒发送消息。
 */
public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
    Message msg = Message.obtain();
    // 系统调用Message.obtain() ,可复用message对象、避免new对象、浪费资源。
    msg.what = what;
    return sendMessageDelayed(msg, delayMillis);
}

/**
 * 在指定时间uptimeMillis 发送消息。
 */
public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis) {
    Message msg = Message.obtain();
    // 系统调用Message.obtain() ,可复用message对象、避免new对象、浪费资源。
    msg.what = what;
    return sendMessageAtTime(msg, uptimeMillis);
}

public final boolean sendMessageDelayed(Message msg, long delayMillis)  {
    if (delayMillis < 0) {
        delayMillis = 0;
    }
    //获取系统时间,加载需要延迟的时间,定时发送消息。
    return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}

public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
    MessageQueue queue = mQueue; //线程中的消息队列。
    if (queue == null) {
        RuntimeException e = new RuntimeException(
                this + " sendMessageAtTime() called with no mQueue");
        Log.w("Looper", e.getMessage(), e);
        return false;
    }
    //将message压入消息队列,保证消息的有序性。
    return enqueueMessage(queue, msg, uptimeMillis);
}
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
    msg.target = this; //msg.target 就是保存Handler对象。
    if (mAsynchronous) {
        msg.setAsynchronous(true);
    }
    return queue.enqueueMessage(msg, uptimeMillis); //将message压入队列中。
}
boolean enqueueMessage(Message msg, long when) {
    if (msg.target == null) {
        throw new IllegalArgumentException("Message must have a target.");
    }
    if (msg.isInUse()) {
        throw new IllegalStateException(msg + " This message is already in use.");
    }

    synchronized (this) { //添加同步,因为MessageQueen 不是线程安全的。
        if (mQuitting) {
            IllegalStateException e = new IllegalStateException(
                    msg.target + " sending message to a Handler on a dead thread");
            Log.w(TAG, e.getMessage(), e);
            msg.recycle(); //如果Looper循环已经退出,则不进入队列。
            return false;
        }

        msg.markInUse(); // 将message对象标记为已用。
        msg.when = when;
        Message p = mMessages; //找到MessageQueen中的最顶部消息。
        boolean needWake;
        if (p == null || when == 0 || when < p.when) {
            // 第一种情况是 MessageQueen中没有消息;
            // 第二个是when为0,传入Message需要马上执行;
            // 第三种情况,MessageQueen中需要马上执行的消息,执行时间在后面。
            // 此三种情况直接将传入Message放在MessageQueen顶部。
            // New head, wake up the event queue if blocked.
            msg.next = p; // 置顶后,将next指向刚刚的消息,排序。
            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 (;;) {
                // 进入循环,直到找到对应的位置。
                //(注:MessageQueen是按执行时间先后排列,越靠后执行的时间越晚。)
                prev = p;
                p = p.next;
                if (p == null || when < p.when) {
                    // p == null 表明已到最好一条消息。when<p.when 找到了时间更大、等得更久的消息。
                    break;
                }
                if (needWake && p.isAsynchronous()) {
                    needWake = false;
                }
            }
            msg.next = p; // invariant: p == prev.next
            prev.next = msg; // 此处为插入消息,将前后连接起来。
        }

        // We can assume mPtr != 0 because mQuitting is false.
        if (needWake) {
            nativeWake(mPtr);
        }
    }
    return true; // 成功放入消息。
}

至此,Message放入指定的位置,消息的发送完成,那么系统又是怎么找到它并执行的呢,这个时候我们需要系统提供的另外一个工具类Looper。

四、Looper - 找到消息。
if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = looper.mQueue; // 传入队列对象。
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));
}
private Looper(boolean quitAllowed) {
    mQueue = new MessageQueue(quitAllowed);
    mThread = Thread.currentThread();
}
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();
        // 取出Message中需要立即执行的对象。
        // 系统提示我们这个方法可能会堵塞,小伙伴们想一想,我们刚刚存储的when,因为消息可能不是需要马上执行的。
        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
        final Printer logging = me.mLogging;
        if (logging != null) {
            logging.println(">>>>> Dispatching to " + msg.target + " " +
                    msg.callback + ": " + msg.what);
        }

        final long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;

        final long traceTag = me.mTraceTag;
        if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
            Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
        }
        final long start = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
        final long end;
        try {
            msg.target.dispatchMessage(msg);
            // msg.target 就是Handler对象。dispatchMessage就是处理消息。
            end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
        } finally {
            if (traceTag != 0) {
                Trace.traceEnd(traceTag);
            }
        }
        if (slowDispatchThresholdMs > 0) {
            final long time = end - start;
            if (time > slowDispatchThresholdMs) {
                Slog.w(TAG, "Dispatch took " + time + "ms on "
                        + Thread.currentThread().getName() + ", h=" +
                        msg.target + " cb=" + msg.callback + " msg=" + msg.what);
            }
        }

        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);
        }
        // 回收消息以便Message.obtain()方法复用,原理暂不涉及。
        msg.recycleUnchecked();
    }
}
Message next() {
    // Return here if the message loop has already quit and been disposed.
    // This can happen if the application tries to restart a looper after quit
    // which is not supported.
    final long ptr = mPtr;
    if (ptr == 0) {
        return null; // Looper退出。
    }

    int pendingIdleHandlerCount = -1; // -1 only during first iteration
    int nextPollTimeoutMillis = 0;
    for (;;) {
        if (nextPollTimeoutMillis != 0) {
            Binder.flushPendingCommands();
        }

        nativePollOnce(ptr, nextPollTimeoutMillis); //文下备注。

        synchronized (this) {
            // Try to retrieve the next message.  Return if found.
            final long now = SystemClock.uptimeMillis();
            Message prevMsg = null;
            Message msg = mMessages;
            if (msg != null && msg.target == null) {
                // Stalled by a barrier.  Find the next asynchronous message in the queue.
                do {
                    prevMsg = msg;
                    msg = msg.next;
                    // 找到时间最久的同步消息。
                } while (msg != null && !msg.isAsynchronous());
            }
            if (msg != null) {
                if (now < msg.when) {
                    // 没有需要立即执行的message,则获取消息需要的等待时间,但不能超过最大值。
                    nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                } else {
                    // Got a message.
                    mBlocked = false;
                    if (prevMsg != null) {
                        prevMsg.next = msg.next;
                    } else {
                        mMessages = msg.next;
                    } // 取出指定Message、并将指针指向下一个消息。
                    msg.next = null; // 断开要被取走消息与其他消息的关系。
                    if (DEBUG) Log.v(TAG, "Returning message: " + msg);
                    msg.markInUse(); // 标记为正在使用。
                    return msg;
                    // 取出有用的消息,直接结束。
                }
            } else {
                // No more messages.
                nextPollTimeoutMillis = -1;
            }

            // Process the quit message now that all pending messages have been handled.
            if (mQuitting) {
                dispose();
                return null;
            }
            .... //其他暂不分析。
        }
    }
}

简要分析一下 nativePollOnce方法,这一个native方法,实际作用就是通过Native层的MessageQueue阻塞nextPollTimeoutMillis毫秒的时间。
分为以下三种情况。

五、总结
欢迎大家,留言谈论。
上一篇 下一篇

猜你喜欢

热点阅读