Android消息机制

132 阅读8分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第25天,点击查看活动详情

1.Android消息机制概述

Android的消息机制主要是指Handler的运行机制以及Handler所附带的MessageQueue和Looper的工作过程,这三者是一个整体。

Handler的运行需要底层的MessageQueue和Looper的支撑。Handler在创建的时候会采用当前线程的Looper来构造消息循环系统,Handler获取Looper是通过ThreadLocal获取的,ThreadLocal可以在不同的线程中互不干扰的存储并提供数据,通过ThreadLocal就可以轻松的获取到Looper,Looper轮询消息队列并获取新的消息,如果没有就等待,MessageQueue就是消息队列,内部存储了一组消息,以队列的形式对外提供插入和删除的操作,虽然称为队列但是它的内部是单链表的结构存储消息列表。

Handler的主要任务是将一个任务切换到指定的线程中去,这是因为Android规定访问UI只能在主线程中操作,如果在子线程中访问UI就会抛出异常。

系统提供Handler的主要目的是解决无法在子线程访问UI的问题,而系统不允许在子线程访问UI是因为UI线程是不安全的,如果在多线程中并发访问可能会导致UI控件处于不可预期的状态,不加锁的原因是这样会导致UI访问的逻辑变得复杂且影响UI的访问效率,锁机制的加入会阻塞某些线程的执行。

Looper运行在创建Handler所在的线程中,这样一来Handler的业务逻辑也就被切换到创建Handler所在的线程中执行了。

2.Android消息机制分析

2.1 ThreadLocal的工作原理

ThreadLocal是一个线程内部的数据存储类,通过它可以在指定的线程中存储数据,数据被存储后想要取出数据也只能取出指定线程的数据,对于其他线程中的数据则无法获取。它的使用场景之一就会Handler,当Handler需要获取当前线程的Looper,很显然Looper的作用域就是线程并且不同的线程具有不同的Looper,这个时候通过ThreadLocal就可以很轻松的实现Looper在线程中的存取。如果不采用ThreadLocal系统就需要提供一个全局的哈希表供Handler查找指定线程,这样一来就必须提供一个类似与LooperManager的类了。

ThreadLocal的另一个使用场景是复杂逻辑下的对象传递,例如监听器的传递,采用ThreadLocal可以让监听器作为线程内部的全局对象而存在,在线程内部只要通过get方法就可以获取到监听器。

不同线程访问同一个TreadLocal的get方法,ThreadLocal内部会从各自的线程中取出一个数组,然后再从数组中根据当前的ThreadLocal的索引找出对应的value值。

先看一下ThreadLocal是如何保存数据的,从set方法看起

//ThreadLocal#set
public void set(T value) {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null)
        map.set(this, value);
    else
        createMap(t, value);
}

void createMap(Thread t, T firstValue) {
    t.threadLocals = new ThreadLocalMap(this, firstValue);
}

ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
    table = new Entry[INITIAL_CAPACITY];
    int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
    table[i] = new Entry(firstKey, firstValue);
    size = 1;
    setThreshold(INITIAL_CAPACITY);
}

//ThreadLocal#set
private void set(ThreadLocal<?> key, Object value) {

    // We don't use a fast path as with get() because it is at
    // least as common to use set() to create new entries as
    // it is to replace existing ones, in which case, a fast
    // path would fail more often than not.

    Entry[] tab = table;
    int len = tab.length;
    int i = key.threadLocalHashCode & (len-1);

    for (Entry e = tab[i];
         e != null;
         e = tab[i = nextIndex(i, len)]) {
        ThreadLocal<?> k = e.get();

        if (k == key) {
            e.value = value;
            return;
        }

        if (k == null) {
            replaceStaleEntry(key, value, i);
            return;
        }
    }

    tab[i] = new Entry(key, value);
    int sz = ++size;
    if (!cleanSomeSlots(i, sz) && sz >= threshold)
        rehash();
}

private Entry[] table;

static class ThreadLocalMap {

    static class Entry extends WeakReference<ThreadLocal<?>> {
        
        Object value;

        Entry(ThreadLocal<?> k, Object v) {
            super(k);
            value = v;
        }
    }
    ...
}

从set源码可以看出,数据经过ThreadLocalMap存入,在map.set方法中遍历table然后比较指向的TreadLocal是否相同,如果相同则赋值。table是一个Entry数组,它的实例化是在ThreadLocalMap中并且在那里给item进行了赋值,而ThreadLocalMap的实例化是在set方法中的createMap方法中,传参为当前ThreadLocal和通过set传入的参数。这里就可以明白ThreadLocal是如何保存Thread和value的。

再看一下保存数据后又是如何获取的,进入get方法查看

public T get() {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null) {
        ThreadLocalMap.Entry e = map.getEntry(this);
        if (e != null) {
            @SuppressWarnings("unchecked")
            T result = (T)e.value;
            return result;
        }
    }
    return setInitialValue();
}

private T setInitialValue() {
    T value = initialValue();
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null)
        map.set(this, value);
    else
        createMap(t, value);
    return value;
}

在get方法中通过ThreadLocal拿到对应的Entry,然后根据Entry获取对应的value并返回,初始值是由initialValue来描述的默认为null。

2.2 消息队列的工作原理

消息队列一般指的是MessageQueue,MessageQueue主要包括插入数据和读取数据两个操作,其中读取数据还伴随着删除操作,插入和读取的方法分别为enqueueMessage和next,其中enqueueMessage的作用是往消息队列中插入一条消息,next的作用是从消息队列中取出一条消息并且将这条消息从消息队列中删除。MessageQueue虽然叫做消息队列但是内部是通过单链表的数据结构来维护消息列表的,单链表的优势在于能够灵活的进行删除和插入操作。

enqueueMessage方法中主要操作就是单链表的插入操作

//MessageQueue#enqueueMessage
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) {
        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();
            return false;
        }

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

        // We can assume mPtr != 0 because mQuitting is false.
        if (needWake) {
            nativeWake(mPtr);
        }
    }
    return true;
}

next方法实现如下

Message next() {
   	...
    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) {
                    // Next message is not ready.  Set a timeout to wake up when it is ready.
                    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;
                    }
                    msg.next = null;
                    if (DEBUG) Log.v(TAG, "Returning message: " + msg);
                    msg.markInUse();
                    return msg;
                }
            } else {
                // No more messages.
                nextPollTimeoutMillis = -1;
            }

           ...
        }

        ...
    }
}

next方法是一个无限循环的方法,如果队列中有新的消息进来就会返回这条信息并从队列中移除,如果没有就阻塞在这里直到有新的消息到来。

2.3 Looper的工作原理

Looper在Android消息机制中的主要作用就是消息循环,它会不停的从MessageQueue中查看是否有新的消息,有则立刻处理,没有则阻塞在那里,在它的构造方法中创建了MessageQueue并且将当前线程的对象保存起来

private Looper(boolean quitAllowed) {
    mQueue = new MessageQueue(quitAllowed);
    mThread = Thread.currentThread();
}

Handler的工作需要Looper,那么如何为一个线程创建一个Looper呢,通过Looper.prepare()即可为当前线程创建一个Looper,然后通过Looper.loop来开启消息循环。

prepare方法主要的作用就是创建一个Looper然后放入ThreadLocal中,通过ThreadLocal将Thread和Looper关联起来。

new Thread("Thread#2") {
    @Override
    public void run() {
        super.run();
        Looper.prepare();
        Handler handler = new Handler();
        Looper.loop();
    }
}.start();

除了prepar方法外还提供了prepareMainLooper方法,它主要在Activitythread中创建Looper使用,本质上也是通过prepare方法实现的,同时也可以在其他地方通过getMainLooper获取这个Looper。

前面讲了创建和启动一个Looper,现在讲一下Looper的退出,Looper的退出有两个方法,一个是quit另一个是quitSafely,区别在于quit会直接退出Looper,而quitSafely只是做了一个退出标记,然后把消息队列中的已有消息处理完毕后才会退出,显然这是更安全的做法。Looper退出后通过Handler发送的消息会失败,send方法会返回false。如果在子线程中创建了Looper那么在不使用它时要通过quit终止Looper,否则这个Looper就会一直处于等待状态。

Looper的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();

    // Allow overriding a threshold with a system prop. e.g.
    // adb shell 'setprop log.looper.1000.main.slow 1 && stop && start'
    final int thresholdOverride =
        SystemProperties.getInt("log.looper."
                                + Process.myUid() + "."
                                + Thread.currentThread().getName()
                                + ".slow", 0);

    boolean slowDeliveryDetected = false;

    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
        final Printer logging = me.mLogging;
        if (logging != null) {
            logging.println(">>>>> Dispatching to " + msg.target + " " +
                            msg.callback + ": " + msg.what);
        }
        // Make sure the observer won't change while processing a transaction.
        final Observer observer = sObserver;

        final long traceTag = me.mTraceTag;
        long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
        long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs;
        if (thresholdOverride > 0) {
            slowDispatchThresholdMs = thresholdOverride;
            slowDeliveryThresholdMs = thresholdOverride;
        }
        final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0);
        final boolean logSlowDispatch = (slowDispatchThresholdMs > 0);

        final boolean needStartTime = logSlowDelivery || logSlowDispatch;
        final boolean needEndTime = logSlowDispatch;

        if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
            Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
        }

        final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0;
        final long dispatchEnd;
        Object token = null;
        if (observer != null) {
            token = observer.messageDispatchStarting();
        }
        long origWorkSource = ThreadLocalWorkSource.setUid(msg.workSourceUid);
        try {
            msg.target.dispatchMessage(msg);
            if (observer != null) {
                observer.messageDispatched(token, msg);
            }
            dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
        } catch (Exception exception) {
            if (observer != null) {
                observer.dispatchingThrewException(token, msg, exception);
            }
            throw exception;
        } finally {
            ThreadLocalWorkSource.restore(origWorkSource);
            if (traceTag != 0) {
                Trace.traceEnd(traceTag);
            }
        }
        if (logSlowDelivery) {
            if (slowDeliveryDetected) {
                if ((dispatchStart - msg.when) <= 10) {
                    Slog.w(TAG, "Drained");
                    slowDeliveryDetected = false;
                }
            } else {
                if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery",
                                msg)) {
                    // Once we write a slow delivery log, suppress until the queue drains.
                    slowDeliveryDetected = true;
                }
            }
        }
        if (logSlowDispatch) {
            showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", 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();
    }
}

loop方法是一个死循环,唯一跳出这个循环的地方就是MessageQueue的next方法返回null,那么在什么情况下next方法会返回null呢,当Looper的quit方法被调用时,Looper就会调用MessageQueue的quit或者quitSafely方法来通知消息队列退出,当消息队列表标记为退出时next方法就返回了null。Looper必须退出否则loop就会一直循环下去。loop调用MessageQueue的next方法获取消息,而next是一个阻塞操作,没有消息时就会一直阻塞在哪里,这也就导致了loop方法一直阻塞在那里。

2.4 Handler的工作原理

Handler的主要任务就是发送和接收消息,发送消息有post和send,post最终会通过send发出消息

public final boolean sendMessage(@NonNull Message msg) {
    return sendMessageDelayed(msg, 0);
}

public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis) {
    if (delayMillis < 0) {
        delayMillis = 0;
    }
    return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}

public boolean sendMessageAtTime(@NonNull 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;
    }
    return enqueueMessage(queue, msg, uptimeMillis);
}

private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg,
                               long uptimeMillis) {
    msg.target = this;
    msg.workSourceUid = ThreadLocalWorkSource.getUid();

    if (mAsynchronous) {
        msg.setAsynchronous(true);
    }
    return queue.enqueueMessage(msg, uptimeMillis);
}

以上代码可以看到Handler经过了多次的send方法调用最后只是向消息队列中插入了一条消息,然后MessageQueue的next方法就会返回这条消息给Looper,Looper接收到消息之后就开始处理了,最终由Looper交给Handler处理,也就是调用了Handler的dispatchMessage方法

public void dispatchMessage(@NonNull 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();
}

dispatchMessage处理消息的过程是首先判断msg.callback是否为null,不为null则交给handleCallback处理,这个callback是一个Runnable对象,这个Runnable对象就是Handle的post所传递的Runnable参数。其次如果msg.callback=null了再判断mCallback是否为null,不为null就调用mCallback的handleMessage方法处理消息,Callback是一个接口

/**
* Callback interface you can use when instantiating a Handler to avoid
* having to implement your own subclass of Handler.
*/
public interface Callback {
   /**
    * @param msg A {@link android.os.Message Message} object
    * @return True if no further handling is desired
    */
    boolean handleMessage(@NonNull Message msg);
}

它的意义就是创建一个Handler时不需要派生一个子类,更简单的来讲就是不用重写handleMessage方法。当mCallback!=null的条件不成立时最后会调用handleMessage方法来处理消息。

3.主线程的消息循环

Android主线程是ActivityThread,在主线程中的main方法中通过Looper.mainPrepareLooper()方法创建主线程的Looper和MessageQueue,并通过Looper.loop()来开启主线程的消息循环。

public static void main(String[] args) {
    Looper.prepareMainLooper();
    
	...
    ActivityThread thread = new ActivityThread();
    thread.attach(false, startSeq);
    
	...
    Looper.loop();
}

主线程的消息循环开始后ActivityThread还需要一个Handler和消息队列进行交互,这个Handler就是Handler的子类H,内部定义了一组消息类型,主要包含的就是四大组件的启动和停止等过程。

ActivityThread通过ApplicationThread和AMS进行进程间的通信,AMS以进程间通信的方法完成ActivityThread的请求后会回调ApplicaitionThread的Binder方法,然后ApplicationThread会向H发送消息,H收到消息后会将ApplicationThread中的逻辑切换到ActivityThread中,也就是切换到了主线程中执行,这个过程就是主线程的消息循环模型。