1. 程式人生 > >Android訊息機制(Handler、MessageQueue和Looper三者的工作原理)

Android訊息機制(Handler、MessageQueue和Looper三者的工作原理)

Android的訊息機制主要是指Handler的執行機制以及Handler所附帶的MessageQueue和Looper的工作過程。messagequeue意思是訊息佇列,它內部儲存一組訊息,有插入和刪除的功能,其實內部是以單鏈表的形式來實現佇列功能的。looper的意思是迴圈,它的主要功能是迴圈讀取messagequeue裡面的訊息,然後加以處理,如果暫時還沒有訊息,則looper會一直等待。在android日常開發中,我們經常利用handler將工作執行緒切換到UI執行緒從而達到更新UI的目的。

在handler機制中,每個執行緒都會對應一個looper,但執行緒預設是沒有looper的,需要自己建立,而我們在android的UI執行緒中可以直接使用handler是因為UI執行緒在建立的時候已經初始化了looper,然後looper物件是通過ThreadLocal儲存在對應thread的threadlocalmap中的。ThreadLocal是什麼?ThreadLocal不是一個執行緒,它的作用是可以為每個不同的執行緒儲存資料,這些不同執行緒的資料互不干擾,像我們的looper就是通過它來儲存在各自執行緒中的,並且可以通過threadlocal來獲取每個執行緒的looper。下面我們來講解它們各自的工作原理。

ThreadLocl的工作原理

先來看一個例子:

public class ThreadLocalDemo {

    private java.lang.ThreadLocal<String> booleanThreadLocal = new ThreadLocal<>();

    public void test() {

        new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                return false;
            }
        });

        booleanThreadLocal.set("主執行緒");

        LogUtils.d(Thread.currentThread().getName() + "的值=" + booleanThreadLocal.get());

        new Thread("執行緒01") {
            @Override
            public void run() {
                super.run();
                booleanThreadLocal.set("執行緒01");
                Log.d("tag", Thread.currentThread().getName() + "的值=" + booleanThreadLocal.get());
            }
        }.start();

        new Thread("執行緒02") {
            @Override
            public void run() {
                super.run();
                //booleanThreadLocal.set("執行緒02");
                Log.d("tag", Thread.currentThread().getName() + "的值=" + booleanThreadLocal.get());
            }
        }.start();

    }
}

執行結果:

通過上面的例子執行結果來看,threadlocal確實可以為不同的執行緒儲存不同的結果,下面我們來看看它的原始碼是怎麼實現的:

    public void set(T value) {
        //獲取當前執行緒
        Thread t = Thread.currentThread();
        //獲取當前執行緒的threadlocalmap
        ThreadLocalMap map = getMap(t);
        //如果threadlocalmap不為null,那麼將value值儲存,其中key值為當前threadlocal
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

   //返回執行緒的threadlocalmap物件 
   ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }

  //建立對應執行緒的threadlocalmap例項,並且儲存對應的值
  void createMap(Thread t, T firstValue) {
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }

其實這裡的關鍵是將value值儲存到當前執行緒的threadlocalmap中去,而且對應的key是threadlocal本身,下面我們來看看threadlocal的get方法:

   public T get() {
        //獲取對應的執行緒
        Thread t = Thread.currentThread();
        //獲取對應執行緒的threadlocalmap
        ThreadLocalMap map = getMap(t);
        //如果不為null,將取出key為當前threadlocal對應的value
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                @SuppressWarnings("unchecked")
                T result = (T)e.value;
                return result;
            }
        }
        //threadlocalmap為null的時候,返回setInitialValue的值,其實就是null
        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;
    }

    protected T initialValue() {
        return null;
    }

get方法其實就是返回我們儲存的value值,當然如果我們之前沒有對應set方法儲存對應值的話,就返回null,這就是為什麼上面的那個例子中,執行緒02會返回null的原因了。好了,其實threadlocal還是比較簡單的。

MessageQueue的工作原理

messagequeue的插入和讀取操作內部是由單鏈表結構來實現的,可能是因為單鏈表結構在插入和刪除上效率比較高吧。插入用enqueueMessage方法,讀取用next方法來實現,下面我們來看看他們的原始碼:

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

上面是訊息的插入操作,注意引數when代表這個訊息何時取出來處理。下面我們看看,next方法原始碼:

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

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

                // Process the quit message now that all pending messages have been handled.
                if (mQuitting) {
                    dispose();
                    return null;
                }

                // If first time idle, then get the number of idlers to run.
                // Idle handles only run if the queue is empty or if the first message
                // in the queue (possibly a barrier) is due to be handled in the future.
                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);
            }

            // Run the idle handlers.
            // We only ever reach this code block during the first iteration.
            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);
                    }
                }
            }

            // Reset the idle handler count to 0 so we do not run them again.
            pendingIdleHandlerCount = 0;

            // While calling an idle handler, a new message could have been delivered
            // so go back and look again for a pending message without waiting.
            nextPollTimeoutMillis = 0;
        }
    }

可以看到,next是一個無限迴圈的方法,當取到訊息的時候返回msg,沒有訊息的時候,會一直阻塞直到有訊息。注意上面的一個判斷,如果now<msg.when,就先不取msg,其實就是說這個訊息還沒到執行時間,所以暫時不會取出來。還有當looper執行退出的時候,即上面的mQuitting為true的時候,此時next方法返回null。理解了messagequeue,下面我們來看看Looper的工作原理。

Looper的工作原理

looper在訊息機制中扮演者訊息迴圈的角色,就是說它會不停地從訊息佇列中檢視有沒有新訊息,如果取出了新訊息就會馬上處理,否則就一直阻塞在那裡。我們知道,開啟looper的工作,需要在對應的執行緒中通過Looper.prepare()建立一個looper,然後通過loop()方法來開啟訊息的迴圈,我們來看看這個兩個方法的原始碼:

    public static void prepare() {
        prepare(true);
    }

    private static void prepare(boolean quitAllowed) {
        //一個執行緒只能建立一個looper
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        //建立一個looper例項,通過threadlocal儲存起來
        sThreadLocal.set(new Looper(quitAllowed));
    }

來看看looper的構造方法:

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

可以看到,構造方法中會建立一個訊息佇列和獲取當前looper的執行緒。looper還提供了prepareMainLooper方法,這個方法是用來給主執行緒ActivityThread建立looper使用的,當然其本質也是通過prepare方法來實現的。由於主執行緒的looper比較特殊,所以其提供了getMainLooper方法,用來在任何地方都可以獲取主執行緒的looper物件。looper提供了兩個退出方法:quit和quitSafely,其中quit是立即退出,quitSafely是等訊息佇列中的訊息處理完畢以後再安全退出。如果我們手動建立了looper,在不用的時候需要我們手動去退出,否則這個子執行緒會一直處於等待狀態。下面我們來看看looper最關鍵的方法loop,只有呼叫了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();

        //無限迴圈
        for (;;) {
            //呼叫訊息佇列的next方法取訊息
            Message msg = queue.next(); // might block
            //如果訊息為null,結束方法
            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 {
                //呼叫對應handler物件的方法,將訊息遞交給handler處理
                msg.target.dispatchMessage(msg);
                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);
            }

            msg.recycleUnchecked();
        }
    }

loop方法還是比較好理解的,首先這是一個死迴圈,唯一一個跳出迴圈的條件是取到的訊息為null,就是當我們呼叫了looper的退出方法的時候,同時也會去呼叫messagequeue的退出方法,此時next方法就會返回一個null值。所以說我們不用的時候必須要手動退出looper,不然這個迴圈會一直持續下去。如果我們取出了新訊息,後面就會呼叫msg.target.dispatchMessage(msg),這裡的target就是傳送訊息的handler物件,這樣訊息又交給了handler來處理了。值得注意的是,我們取出訊息呼叫handler的dispatchMessage是在looper的loop方法中執行的,而looper物件是儲存在建立時候的那個執行緒中的,所以此時就成功地實現了執行緒切換。講解了訊息佇列和looper的工作原理,下面我們來看看Handler是怎麼工作的。

Handler的工作原理

handler的工作主要包括訊息的傳送和接收,訊息的傳送主要用post和send系列方法,接收一般是通過重寫對應的handleMessage方法。下面我們先來看看一個例子:

public class HandlerDemo {


    //重寫handler的handleMessage方法
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            LogUtils.d("handler接收到訊息,what內容=" + msg.what);
        }
    };

    //通過傳遞一個callback實現來處理訊息
    Handler handler1 = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            return false;
        }
    });

    public void sendMsg() {
        //在一個子執行緒中通過主執行緒的handler傳送訊息給主執行緒處理
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //延時兩秒
                    TimeUnit.SECONDS.sleep(2);
                    //傳送一個訊息給handler處理
                    Message msg = new Message();
                    msg.what = 01;
                    handler.sendMessage(msg);

                } catch (InterruptedException e) {

                }
            }
        }).start();


    }

    public void post() {
        handler1.post(new Runnable() {
            @Override
            public void run() {
                LogUtils.d("handler1的post方法執行了run方法");
            }
        });
    }

}

執行結果:

可以看到,例子中建立了主執行緒的handler物件,演示了在子執行緒中通過sendMessage方法傳送訊息給主執行緒處理還有post方法的使用,下面我們來看看它們的原始碼是怎麼實現的。

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

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

其實上面的最終關鍵的就是把訊息儲存到對應的訊息佇列中,值得注意的時候,msg.target=this,這裡給target賦值為handler本身。我們來看看這個訊息佇列是怎麼來的

    public Handler(Callback callback, boolean async) {
        if (FIND_POTENTIAL_LEAKS) {
            final Class<? extends Handler> klass = getClass();
            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                    (klass.getModifiers() & Modifier.STATIC) == 0) {
                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
                    klass.getCanonicalName());
            }
        }
        //獲取對應的looper例項
        mLooper = Looper.myLooper();
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
        }
        //獲取訊息佇列
        mQueue = mLooper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
    }

可以看到,在handler的構造方法中,我們獲得了執行緒對應的looper,如果是主執行緒當然就是獲得主執行緒的looper了,然後通過looper獲得訊息佇列mQueue。

在講looper的時候,我們知道loop方法會一直去訊息佇列中取訊息,一旦取出新訊息,就會呼叫handler的dispatchMessage方法,下面我們來看看這個方法:

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

其中上面的msg.callback就是post方法中的runnable物件,我們來看看:

    public final boolean post(Runnable r)
    {
       return  sendMessageDelayed(getPostMessage(r), 0);
    }

  private static Message getPostMessage(Runnable r) {
        Message m = Message.obtain();
        m.callback = r;
        return m;
    }

再來看看handleCallback(msg)

   private static void handleCallback(Message message) {
        message.callback.run();
    }

可以看到會呼叫runnable物件的run方法,這就是handler的post方法的原理。那mCallback 又是怎麼來的

    public Handler(Callback callback) {
        this(callback, false);
    }

    public Handler(Callback callback, boolean async) {
        if (FIND_POTENTIAL_LEAKS) {
            final Class<? extends Handler> klass = getClass();
            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                    (klass.getModifiers() & Modifier.STATIC) == 0) {
                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
                    klass.getCanonicalName());
            }
        }

        mLooper = Looper.myLooper();
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;
        //給mCallback賦值
        mCallback = callback;
        mAsynchronous = async;
    }

    //Callback介面
    public interface Callback {
        /**
         * @param msg A {@link android.os.Message Message} object
         * @return True if no further handling is desired
         */
        public boolean handleMessage(Message msg);
    }

可以看到,其實這個mCallback就是我們在例子中建立handler1的時候傳遞的引數。如果這個回撥方法為null,我們就呼叫handleMessage(msg)方法處理,這是一個public方法裡面為空實現,需要我們自己重寫去處理,這個就是我們例子中建立handler的時候實現。

好了,關於android訊息機制的工作原理,通過上面的講解我們大概也清楚是怎麼一回事了。