一文捋清Android消息機制

前言

消息機制可稱爲Handler機制,Android中的視圖繪製,事件傳遞,四大組件的生命週期都離不開Handler機制,都是由Handler機制直接或間接的完成。java

重要組件

  • ThreadLocal 每一個線程獨有的本地變量,線程中止時,線程獨有的變量將消失 用於存儲線程對應的Looper
  • Looper 用於消息循環的類,由它來源源不斷的從MessageQueue中取出Message,並交由Handler處理,與線程關聯,每一個線程只有一個實例
  • MessageQueue 消息隊列 Looper不斷從MessageQueue中獲取Message,Handler發送消息交由MessageQueue排隊,每一個線程只有一個實例
  • Handler 發送消息和處理消息的類 發送消息交由MessageQueue去排隊,Looper拿到消息會交於對應的Handler去處理,每一個線程可有多個
  • Message 消息對象 承載消息數據,而且包含target對象指向發送該消息的Handler

下面是類圖(第一次畫類圖,哈哈哈...)
緩存

類圖

消息流程

先放一張流程圖
bash

消息流程圖

1.Looper.prepare()

準備循環,主要是新建一個Looper並與本地線程進行綁定,具體源碼以下less

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));
    }
複製代碼

有兩個重載方法,默認傳入的true,quitAllowed是否運行退出,主線程是不容許退出的。
內容比較簡單,建立一個Looper並設置給mThreadLocal。那mThreadLocal是何方神聖呢?異步

// sThreadLocal.get() will return null unless you've called prepare(). static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); 複製代碼

可看到它被static final修飾,進程中全局只有一個。泛型是Looper的ThreadLocal對象,看下ThreadLocal的註釋async

* This class provides thread-local variables.  These variables differ from
 * their normal counterparts in that each thread that accesses one (via its
 * {@code get} or {@code set} method) has its own, independently initialized
 * copy of the variable.  {@code ThreadLocal} instances are typically private
 * static fields in classes that wish to associate state with a thread (e.g.,
 * a user ID or Transaction ID).
複製代碼

提供線程的本地變量,每個線程都是獨有的 再看下它的set方法ide

public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }
    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }
    void createMap(Thread t, T firstValue) {
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }

複製代碼

獲取當前線程,並獲取線程中的threadLocals變量(該變量可看作一個Map集合),並將ThreadLocal自己爲Key,set的值爲value存入。由此看出神奇的ThreadLocal實際上經過Thread對象中的threadLocals變量保證了保存的變量在每一個線程的惟一性。oop

繼續看下Looper的構造方法post

private Looper(boolean quitAllowed) {
    	//建立一個MessageQueue
        mQueue = new MessageQueue(quitAllowed);
        //保存當前線程對象
        mThread = Thread.currentThread();
    }
複製代碼

2.Looper.loop()

開啓循環,主要代碼以下ui

public static void loop() {
 		//1.獲取當前線程的Looper
        final Looper me = myLooper();
        if (me == null) {
            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
        }
        //2.獲取Looper中的MessageQueue對象
        final MessageQueue queue = me.mQueue;
		···
        for (;;) {
        	//3.從MessageQueue中獲取下一條要處理的消息  queue.next是阻塞方法
            Message msg = queue.next(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }
            ···
            try {
            	//4.將獲取的消息交於 msg.target處理 msg.target實際存儲了發送該消息的Handler對象
                msg.target.dispatchMessage(msg);
            } 
            ···
            5.消息回收
            msg.recycleUnchecked();
        }
    }
複製代碼

該方法主要有四個步驟:

  1. 獲取當前線程的Looper
  2. 獲取Looper中的MessageQueue對象
  3. 從MessageQueue中獲取下一條要處理的消息 queue.next是阻塞方法
  4. 將獲取的消息交於 msg.target處理 msg.target實際存儲了發送該消息的Handler對象
  5. 消息使用完畢,進行回收,消息回收後面有講到
    重複執行步驟三、4源源不斷的從消息隊列獲取消息並處理消息

3.handler.send...和handler.post...

貼幾個關鍵方法吧

public final boolean postAtTime(Runnable r, long uptimeMillis){
        return sendMessageAtTime(getPostMessage(r), uptimeMillis);
    }
   private static Message getPostMessage(Runnable r) {
        Message m = Message.obtain();
        m.callback = r;
        return m;
    }
   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;
        }
        return enqueueMessage(queue, msg, uptimeMillis);
    }
    private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }
複製代碼

能夠看出全部的發送消息方法post和send最後都會調用sendMessageAtTime方法,而後調用queue.enqueueMessage(msg, uptimeMillis)方法,將Handler自身綁定到Message的target變量上
post方法有個不一樣點是將Message.callback賦值爲傳入的Runnable對象,這塊後面有用到。

4.queue.enqueueMessage

MessageQueue的enqueueMessage方法

boolean enqueueMessage(Message msg, long when) {
  		//target不容許爲空
        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) {
                msg.recycle();
                return false;
            }
            //將msg標記爲正在使用中
            msg.markInUse();
            //設置msg的觸發時間
            msg.when = when;
            // 將P變量賦值爲mMessages,mMessage是消息隊列中的第一個消息
            Message p = mMessages;
            //是否須要喚醒  
            boolean needWake;
            //若是消息隊列中沒有消息 或者 新消息的觸發時間在消息隊列的第一個消息以前時
            //則新消息放入消息隊列頭部。若是當前正在阻塞,則須要喚醒  needWake = mBlocked
            if (p == null || when == 0 || when < p.when) {
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked;
            } else {
                // 若是當前是阻塞狀態 && 第一個消息是柵欄消息 && 新消息時異步消息時 須要喚醒 (柵欄後面會講)
                needWake = mBlocked && p.target == null && msg.isAsynchronous();

                //根據when 從小到大的順序 找到新消息在隊列中的位置 並插入到消息隊列中
                Message prev;
                for (;;) {
                    prev = p;
                    p = p.next;
                    if (p == null || when < p.when) {
                        break;
                    }

                    //若是須要喚醒 && p是異步消息 則取消喚醒  這是由於新消息不是第一個要執行的異步消息,不用喚醒。
                    if (needWake && p.isAsynchronous()) {
                        needWake = false;
                    }
                }
                msg.next = p; // invariant: p == prev.next
                prev.next = msg;
            }

            
            if (needWake) {
            	//調用本地方法喚醒
                nativeWake(mPtr);
            }
        }
        return true;
    }
複製代碼

從上面的源碼能夠看出,MessageQueue.enqueueMessage方法,是將新消息按照消息的觸發時間when進行隊列,找到新消息的具體位置,再根據當前的狀態判斷是否進行喚醒操做(實際上無論同步和異步消息,若是新消息是下一次要執行的消息時就會進行喚醒操做。)異步消息後面會講,它和柵欄有一些關係

5.queue.next()

咱們從上面知道Looper.loop()方法會一直調用queue.next()取出消息進行處理,先放下queue.next()的源碼

Message next() {
       	//mPtr和 native層的NavtiveMessageQueue 關聯
        final long ptr = mPtr;
        if (ptr == 0) {
            return null;
        }

        int pendingIdleHandlerCount = -1; // -1 only during first iteration
        int nextPollTimeoutMillis = 0;
        for (;;) {//無限循環
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }
            //1.調用本地阻塞方法,nextPollTimeoutMillis爲阻塞時長,-1 會一直阻塞,直到調用ativeWake(mPtr)喚醒
            nativePollOnce(ptr, nextPollTimeoutMillis);

            synchronized (this) {
                // Try to retrieve the next message.  Return if found.
                final long now = SystemClock.uptimeMillis();
                Message prevMsg = null;

                //2.取出下一次要執行的消息
                Message msg = mMessages;//取出入隊列的第一個元素
                if (msg != null && msg.target == null) {
                    // 若是該消息是柵欄消息,則獲取第一個異步消息
                    do {
                        prevMsg = msg;
                        msg = msg.next;
                    } while (msg != null && !msg.isAsynchronous());
                }

                //3.根據下一條要執行的消息when 判斷是否須要返回當前該條消息
                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 != null  取出第一個異步消息後,保證隊列的順序
                            prevMsg.next = msg.next;
                        } else {
                            mMessages = msg.next;
                        }
                        msg.next = null;
                    
                        msg.markInUse();
                        return msg;
                    }
                } else {
                    // 若是沒有消息,設置下一次超時時間爲-1 會一直阻塞,直到調用ativeWake(mPtr)喚醒
                    nextPollTimeoutMillis = -1;
                }

                // 正在退出時會返回空
                if (mQuitting) {
                    dispose();
                    return null;
                }

                //4.mIdleHandlers 空閒任務處理
               	//pendingIdleHandlerCount 表示將要處理的空閒任務數量  只有第一次執行循環時 pendingIdleHandlerCount<0  
               	// mIdleHandlers 全部的空閒任務  調用一次next方法至多一次會進入該判斷
                if (pendingIdleHandlerCount < 0
                        && (mMessages == null || now < mMessages.when)) {
                    pendingIdleHandlerCount = mIdleHandlers.size();
                }
                //若是沒有待處理的空閒任務 則繼續循環
                if (pendingIdleHandlerCount <= 0) {
                    // No idle handlers to run.  Loop and wait some more.
                    mBlocked = true;
                    continue;
                }
                //取出待處理的任務
                if (mPendingIdleHandlers == null) {
                    mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
                }
                mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
            }

            //若是執行到這裏表示 有待執行的空閒任務 而且當前沒有Message要處理

           	//執行待處理的任務
            for (int i = 0; i < pendingIdleHandlerCount; i++) {
                final IdleHandler idler = mPendingIdleHandlers[i];
                mPendingIdleHandlers[i] = null; // release the reference to the handler

                boolean keep = false;
                try {
                    keep = idler.queueIdle();
                } catch (Throwable t) {
                    Log.wtf(TAG, "IdleHandler threw exception", t);
                }

                if (!keep) {
                    synchronized (this) {
                        mIdleHandlers.remove(idler);
                    }
                }
            }

            //重置待執行空閒任務的數量
            pendingIdleHandlerCount = 0;

            //將阻塞時間設置爲0 立刻進行下一次消息的取出 
            nextPollTimeoutMillis = 0;
        }
    }

複製代碼

主要分4大塊

  1. 調用本地方法nativePollOnce(ptr, nextPollTimeoutMillis),該方法會阻塞nextPollTimeoutMillis時長,當值爲-1時,會一直阻塞,直到被喚醒
  2. 取出下一次要執行的消息
  3. 根據下一條要執行的消息when 判斷是否須要返回當前該條消息,若是when小於等於當前時間,則返回,不然繼續下次循環
  4. 若是當前沒有要處理的消息,則執行mIdleHandlers 空閒任務

** 空閒任務IdleHandle **

MessageQueue.java

    public void addIdleHandler(@NonNull IdleHandler handler) {
        if (handler == null) {
            throw new NullPointerException("Can't add a null IdleHandler");
        }
        synchronized (this) {
            mIdleHandlers.add(handler);
        }
    }
    public void removeIdleHandler(@NonNull IdleHandler handler) {
        synchronized (this) {
            mIdleHandlers.remove(handler);
        }
     public static interface IdleHandler {
        boolean queueIdle();
    }

複製代碼

MessageQueue提供了兩個關係IdleHandler的方法,一個是添加一個是刪除
IdleHandler是一個接口,該接口只有一個queueIdle方法,返回一個boolean,若是返回false,該IdleHandler掉用過一次即被移除掉,不然每次調用next方法,進入空閒狀態都會執行一次IdleHandler

5.handler.dispatchMessage(msg)

從上面得知MessageQueue.next()方法返回要處理的message時,會在Looper.loop()方法的循環內部調用msg.target.dispatchMessage(msg),咱們知道msg.target其實是發送該msg的Handler,下面是handler.dispatchMessage(msg)源碼

public void dispatchMessage(Message msg) {
    	//經過post... 方法發送的消息 msg.callback!=null
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
        	//mCallback能夠經過Handler的構造方法傳入  若是mCallback.handleMessage(msg)返回true表示該消息已消費處理
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            //調用自身的handlerMessage  通常咱們自定義Handler時會重寫該方法
            handleMessage(msg);
        }
    }
     private static void handleCallback(Message message) {
        message.callback.run();
    }
複製代碼

從上面的源碼能夠看出,日常咱們在使用自定義Handler時,若是重寫了handlerMessage方法,執行的條件是,經過send方法發送消息,而且沒有傳入mCallback或者傳入的mCallback的handlerMessage方法返回false

柵欄和異步消息

柵欄也可叫作同步柵欄,做用是攔截同步消息,放行異步消息。MessageQueue有下面幾個關於柵欄的方法

MessageQueue.java
	
 	public int postSyncBarrier() {
        return postSyncBarrier(SystemClock.uptimeMillis());
    }
    //加入一個同步柵欄
    private int postSyncBarrier(long when) {
        synchronized (this) {
            //獲取一個token mNextBarrierToken從0開始自增加
            final int token = mNextBarrierToken++;
            //建立一個柵欄消息 能夠看出並無設置msg.target 其實msg是不是柵欄消息就是根據msg.target==null判斷的
            final Message msg = Message.obtain();
            msg.markInUse();
            msg.when = when;
            msg.arg1 = token;

            //根據柵欄消息的時間,找到柵欄消息在消息隊列中的位置
            Message prev = null;
            Message p = mMessages;
            if (when != 0) {
                while (p != null && p.when <= when) {
                    prev = p;
                    p = p.next;
                }
            }
            if (prev != null) { // invariant: p == prev.next
                msg.next = p;
                prev.next = msg;
            } else {
                msg.next = p;
                mMessages = msg;
            }
            //返回token  做爲一個柵欄標記 用於取消柵欄
            return token;
        }
    }

    /**
     *根據柵欄的token值移除柵欄消息
     * @hide
     */
    public void removeSyncBarrier(int token) {
        // Remove a sync barrier token from the queue.
        // If the queue is no longer stalled by a barrier then wake it.
        synchronized (this) {
            //找出柵欄消息和柵欄的前一個消息
            Message prev = null;
            Message p = mMessages;
            while (p != null && (p.target != null || p.arg1 != token)) {
                prev = p;
                p = p.next;
            }
            if (p == null) {
                throw new IllegalStateException("The specified message queue synchronization "
                        + " barrier token has not been posted or has already been removed.");
            }
            final boolean needWake;
            if (prev != null) {//若是柵欄不是第一個消息 則表示柵欄消息還沒在執行到 因此不須要進行喚醒
                prev.next = p.next;
                needWake = false;
            } else {//若是第一個是要移除的柵欄消息,則移除消息 移除後消息隊列的第一個消息不是柵欄消息則進行喚醒
                mMessages = p.next;
                needWake = mMessages == null || mMessages.target != null;
            }
            //回收消息
            p.recycleUnchecked();
            if (needWake && !mQuitting) {
                nativeWake(mPtr);
            }
        }
    }
複製代碼
  1. 加入柵欄時,根據柵欄的時間when,按照時間順序插入到消息隊列中並返回柵欄消息的標記token
  2. 刪除柵欄時,根據柵欄消息的token,刪除柵欄消息,若是沒有執行到該消息,則不須要喚醒操做。若是執行到了,而且下一條消息不是柵欄消息則進行喚醒操做

同步柵欄功能的實現,再看上面的MessageQueue.next()方法

Message next() {
      	···
        for (;;) {
            ···
            synchronized (this) {
             	 ···
                Message msg = mMessages;
                //若是執行的該條消息是柵欄消息,則獲取隊列中的異步消息  msg.target == null即爲同步柵欄消息
                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());
                }
     }
複製代碼

能夠看出同步柵欄的做用是 攔截柵欄後面同步消息,放行異步消息
放張圖就明白了

柵欄說明圖

紅色的柵欄消息 藍色的爲同步消息 綠色的爲異步消息

Message消息池

Message的構造方法上有下面這些註釋

/*While the constructor of Message is public, the best way to get
 * one of these is to call {@link #obtain Message.obtain()} or one of the
 * {@link Handler#obtainMessage Handler.obtainMessage()} methods, which will pull
 * them from a pool of recycled objects
 */
複製代碼

當前咱們須要構造Message時,最好的方式是調用Message.obtain() 或者Handler#obtainMessage 方法的一個, 這樣會複用消息池的對象
Handler#obtainMessage 方法最終調用的也是Message.obtain()方法,那麼看下Message.obtain()方法的源碼

private static Message sPool;
  public static Message obtain() {
        synchronized (sPoolSync) {
            if (sPool != null) {//消息池不等於空
                //取出消息池的第一個元素 而且將緩存數量-1 並返回
                Message m = sPool;
                sPool = m.next;
                m.next = null;
                m.flags = 0; // clear in-use flag
                sPoolSize--;
                return m;
            }
        }
        //若是消息池沒有消息 則新建一個消息
        return new Message();
    }
複製代碼

能夠看出sPool便是消息池,而且是靜態的,經過靜態實現緩存效果。調用obtain方法時,會從消息池中取出第一個消息,若是消息爲空,則新建一個Message

那麼他在哪裏賦值的呢? 咱們看Looper.loop()方法,在掉msg.target.dispatchMessage(msg)後面會調用一個 msg.recycleUnchecked() 方法,咱們看下這個方法

void recycleUnchecked() {
     
        // 清除全部標記
        flags = FLAG_IN_USE;
        what = 0;
        arg1 = 0;
        arg2 = 0;
        obj = null;
        replyTo = null;
        sendingUid = -1;
        when = 0;
        target = null;
        callback = null;
        data = null;

        synchronized (sPoolSync) {
            if (sPoolSize < MAX_POOL_SIZE) { //若是消息池數量小於MAX_POOL_SIZE  MAX_POOL_SIZE=50
            	//把消息自身做爲消息池的第一個元素,消息池數量+1
                next = sPool;
                sPool = this;
                sPoolSize++;
            }
        }
    }
複製代碼

來張圖說明一下,左邊是MessageQueue 右邊是sPool消息池

消息池

完整流程圖

這張流程圖包括Native層,後期有時間會寫篇關於Native消息機制的文章。
(點擊圖片查看大圖)

完整流程圖
相關文章
相關標籤/搜索