Handler消息機(jī)制

轉(zhuǎn)載請注明出處:http://www.itdecent.cn/p/5e59a9edcc1e

??Handler經(jīng)常被我們用來在主線程和子線程之間傳遞消息,因此這里打算從源碼的角度分析下它的使用,也算是進(jìn)軍源碼的一個(gè)開端!為了使整個(gè)分析過程比較的有條理,便從一個(gè)最簡單的使用場景(代碼如下:)著手,順著源碼來看Message的傳遞流程。

public class MainActivity extends AppCompatActivity {

    public static final String MSG = "msg";
    // 注意內(nèi)存泄露的問題
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(MainActivity.this, msg.getData().getString(MSG), Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(2000);
                Message msg = Message.obtain();
                Bundle bundle= new Bundle();
                bundle.putString(MSG,"子線程發(fā)送的消息!");
                msg.setData(bundle);
                handler.sendMessage(msg);
            }
        }).start();
    }
}

Handler中的消息處理機(jī)制主要包含四個(gè)類:

  1. Message消息對象
  2. Handler消息處理器
  3. MessageQueue消息隊(duì)列
  4. Looper消息輪詢器

下面是用這四個(gè)類畫的時(shí)序圖:7-12步是在子線程中執(zhí)行的,其他在主線中執(zhí)行。
Handler消息機(jī)制

一、消息機(jī)制的初始化


??在啟動(dòng)UI主線程ActivityThread(\frameworks\base\core\java\android\app\ActivityThread.java)會(huì)執(zhí)行下面的main方法:

public static void main(String[] args) {
    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");

    // CloseGuard defaults to true and can be quite spammy.  We
    // disable it here, but selectively enable it later (via
    // StrictMode) on debug builds, but using DropBox, not logs.
    CloseGuard.setEnabled(false);

    Environment.initForCurrentUser();

    // Set the reporter for event logging in libcore
    EventLogger.setReporter(new EventLoggingReporter());

    // Make sure TrustedCertificateStore looks in the right place for CA certificates
    final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
    TrustedCertificateStore.setDefaultUserDirectory(configDir);

    Process.setArgV0("<pre-initialized>");

    Looper.prepareMainLooper();

    ActivityThread thread = new ActivityThread();
    thread.attach(false);

    if (sMainThreadHandler == null) {
        // 創(chuàng)建H extends Handler對象,H類中定義了許多跟Activity生命周期相關(guān)的常量。
        sMainThreadHandler = thread.getHandler();
    }

    if (false) {
        Looper.myLooper().setMessageLogging(new
                        LogPrinter(Log.DEBUG, "ActivityThread"));
    }

    // End of event ActivityThreadMain.
    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
    Looper.loop();

    throw new RuntimeException("Main thread loop unexpectedly exited");
}

1.1,Looper.prepareMainLooper()

在main方法中調(diào)用了Looper.prepareMainLooper(),從下面的源碼中可以看出最終調(diào)用的還是prepare()方法,并且用ThreadLocal保存了Looper的對象,這樣能夠保證每個(gè)Looper對象都與線程對應(yīng),并且從拋出的異常信息中可以知道消息機(jī)制的一個(gè)重要特點(diǎn):每個(gè)線程中只能創(chuàng)建一個(gè)Looper對象。

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

public static void prepareMainLooper() {
    prepare(false);
    synchronized (Looper.class) {
        if (sMainLooper != null) {
            throw new IllegalStateException("The main Looper has already been prepared.");
        }
        sMainLooper = myLooper();
    }
}

Looper的構(gòu)造方法中創(chuàng)建了MessageQueue的實(shí)例

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

Looper.prepare()方法實(shí)例化了Looper和MessageQueue對象。

1.2,Looper.loop()

在main方法的最后執(zhí)行Looper.loop()進(jìn)入死循環(huán),其中queue.next()是一個(gè)阻塞式方法,直到消息隊(duì)列中獲取到Message對象才會(huì)執(zhí)行后面的dispatchMessage(msg)來處理消息。

    /**
     * Run the message queue in this thread. Be sure to call
     * {@link #quit()} to end the 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;
        ......

        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);
            } finally {
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }
            ......
            // 將msg回收到對象池中,便于下次復(fù)用
            msg.recycleUnchecked();
        }
    }

二、Handler的創(chuàng)建

Handler的創(chuàng)建除了示例中的匿名內(nèi)部類方式,還可以通過實(shí)現(xiàn)Callback的方式來創(chuàng)建。這兩種方法本質(zhì)上是一樣的,只不過一提到接口就想到多繼承擴(kuò)展啥的。另外,在消息的處理上略有不同,在后面會(huì)講到。

    public Handler() {
        this(null, 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 = callback;
        mAsynchronous = async;
    }

三、Message的創(chuàng)建

3.1,Message的數(shù)據(jù)結(jié)構(gòu)

先看Message類的定義,Message中除了包括傳遞數(shù)據(jù)所用的變量what、data、when外,還定義了一個(gè)next的引用,這說明Message是一個(gè)鏈表的數(shù)據(jù)結(jié)構(gòu),而MessageQueue雖說叫一個(gè)消息隊(duì)列,實(shí)際上是個(gè)鏈表。另外,Messge中為了避免重復(fù)的創(chuàng)建Message對象造成內(nèi)存的抖動(dòng),也提供了一個(gè)復(fù)用的對象池,最多容納50個(gè)對象。

public final class Message implements Parcelable {
    public int what;
    ......
    /*package*/ long when;
    /*package*/ Bundle data;
    /*package*/ Handler target;
    /*package*/ Runnable callback;
    
    // sometimes we store linked lists of these things
    /*package*/ Message next;
    private static Message sPool;
    private static int sPoolSize = 0;

    private static final int MAX_POOL_SIZE = 50;
    ......
}

3.2,Message.obtain()

該方法中會(huì)先判斷對象池中是否有可復(fù)用的Message對象,如果有就從鏈表頭部取出一個(gè)Message對象返回,否則就會(huì)重新創(chuàng)建一個(gè)Message對象。除了這個(gè)無參的方法,還有7個(gè)重載方法,這些重載方法無非就是加了一些初始化參數(shù)并且都調(diào)用了obtain()方法。

    /**
     * Return a new Message instance from the global pool. Allows us to
     * avoid allocating new objects in many cases.
     */
    public static Message obtain() {
        synchronized (sPoolSync) {
            if (sPool != null) {
                Message m = sPool;
                sPool = m.next;
                m.next = null;
                m.flags = 0; // clear in-use flag
                sPoolSize--;
                return m;
            }
        }
        return new Message();
    }

四、Message的發(fā)送

4.1,sendMessage()

消息創(chuàng)建完后就可以通過handler將消息發(fā)送出去,主要有sendXxx(),sendEmptyXxx(),postXxx()三類方法,sendXxx方法是在調(diào)用之前就初始化好了Message對象,后面兩類方法是在執(zhí)行過程中封裝的Message對象。

  • sendXxx():該系列方法最終調(diào)用的都是sendMessageAtTime(msg,uptimeMills)。從源代碼中可以知道延遲執(zhí)行的時(shí)間不能小于0,并且要求MessageQueue必須先實(shí)例化。
    // 發(fā)送立即處理的消息
    public final boolean sendMessage(Message msg)
    {
        return sendMessageDelayed(msg, 0);
    }
    // 發(fā)送延遲執(zhí)行的消息
    public final boolean sendMessageDelayed(Message msg, long delayMillis)
    {
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }
    // 發(fā)送在制定時(shí)間點(diǎn)執(zhí)行的消息
    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);
    }
    // 特殊情況下使用,該方法僅僅是將uptimeMills置為0
    public final boolean sendMessageAtFrontOfQueue(Message msg) {
        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, 0);
    }
  • sendEmptyXxx()和postXxx():該系列的方法基本上都有與之對應(yīng)的sendXxx()方法。
    public final boolean sendEmptyMessage(int what);
    public final boolean sendEmptyMessageDelayed(int what, long delayMillis);
    public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis);
    // Runnable對象會(huì)賦值給callback
    public final boolean post(Runnable r);
    public final boolean postAtTime(Runnable r, long uptimeMillis);
    public final boolean postAtTime(Runnable r, Object token, long uptimeMillis);
    public final boolean postDelayed(Runnable r, long delayMillis);
    public final boolean postAtFrontOfQueue(Runnable r);

這里以post(Runnable r)方法為例,其它的都差不多。msg中的callback指向了該Runnable對象,后面在處理的時(shí)候會(huì)直接調(diào)用里面的run方法。

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

4.2,enqueueMessage()

從上面的的sendMessageAtTime()得知會(huì)調(diào)用enqueueMessage(),源碼如下。在這里將當(dāng)前Handler的引用this賦值給了msg.target(其實(shí)這是為了避免在創(chuàng)建多個(gè)Handler對象的情況下消息的沖突)。

    private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }

MessageQueue中enqueueMessage()需要遍歷單鏈表將msg插入到鏈表中,并且還要判斷是否該喚醒主線程,即next()方法。

    boolean enqueueMessage(Message msg, long when) {
        ......
        synchronized (this) {
            .....
            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 {
                // 將msg插入到隊(duì)列中
                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;
    }

五、Message的處理

5.1,next()

當(dāng)執(zhí)行nativeWake(mPtr)方法喚醒了主線程,MessageQueue的next()方法(該方法是在Looper.loop()中調(diào)用的,前面有相關(guān)源碼)就會(huì)從鏈表中取出消息。

    Message next() {
        ......
            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;
                    }
                ......
        }
    }

5.2,dispatchMessage()

Looper.loop()方法中會(huì)調(diào)用dispatchMessage(),具體的源碼如下。并且這個(gè)方法是在主線程中調(diào)用的,因此消息就從子線程傳遞到了UI線程中啦。

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

分為三種情況來處理消息:

  • post一個(gè)Runnable對象:該方式會(huì)執(zhí)行handleCallback(),即執(zhí)行Runnable中run方法。
    private static void handleCallback(Message message) {
        message.callback.run();
    }
  • 實(shí)現(xiàn)Callback接口:mCallback是在Handler創(chuàng)建的時(shí)候初始化的,需要自己去實(shí)現(xiàn)這個(gè)內(nèi)部接口。需要注意的是:這個(gè)接口中的方法比Handler中的空方法void handleMessage(Message msg){}多了個(gè)boolean類型的返回值。
    public interface Callback {
        public boolean handleMessage(Message msg);
    }
  • 發(fā)送普通的Message對象:該方式直接調(diào)用自己實(shí)現(xiàn)的handleMessage()方法。當(dāng)然,這個(gè)方法的執(zhí)行與否還與前面的兩種情況有關(guān)。

5.3,recycleUnchecked()

在msg消息處理完成之后,就會(huì)執(zhí)行recycleUnchecked()方法完成的這個(gè)msg的復(fù)用。該方法會(huì)將msg對象中成員變量還原,并采用頭插法將msg插入到對象池(Message.sPool)中。

    void recycleUnchecked() {
        // Mark the message as in use while it remains in the recycled object pool.
        // Clear out all other details.
        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) {
                next = sPool;
                sPool = this;
                sPoolSize++;
            }
        }
    }

六、總結(jié)

??Handler不僅僅只有上面一種使用場景,還可以用于子線程和子線程,子線程和主線程的通信,只是在使用時(shí)要注意以下幾點(diǎn):

  1. 在子線程使用時(shí)中需調(diào)用Looper.prepare()方法來初始化Looper和MessageQueue;
  2. 在子線程使用時(shí)中需調(diào)用Looper.loop()方法來輪詢消息隊(duì)列;
  3. 一個(gè)線程中只能有一個(gè)Looper對象;
  4. 一個(gè)線程中可以創(chuàng)建多個(gè)Handler對象;
  5. 第五部分中消息處理的集中情況(本來想畫個(gè)流程圖但是源碼中已經(jīng)很清晰就懶得畫了)

參考資料
Android7.0源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容