Android 消息機(jī)制之Message

Android 消息機(jī)制之Message

@(Android)

Android的消息機(jī)制中,Message的使用是很頻繁的,處理消息,處理事件,處理其他我還沒(méi)有探索的新世界。在Android的消息機(jī)制中,Message相當(dāng)于承載消息的載體,Handler就是Message的處理者,而MessageQueue和Looper就是Message的搬運(yùn)工。

在Message的內(nèi)部有一個(gè)flag的標(biāo)志位,用于標(biāo)識(shí)當(dāng)前Message的狀態(tài)。有一個(gè)標(biāo)志位是FLAG_IN_USE ,當(dāng)Message在隊(duì)列中,正在給Handler處理的時(shí)候,就會(huì)加上這個(gè)標(biāo)志位,表示當(dāng)前的Message正在使用,那么為什么要記錄Message是否正在被使用呢。因?yàn)镸essage內(nèi)部有一個(gè)對(duì)Message的回收機(jī)制。

因?yàn)镸essage作為Android系統(tǒng)消息的載體,那么若有大量消息需要處理的時(shí)候,就需要大量的Message,就需要不斷的new Message嗎,顯然這樣的很浪費(fèi)時(shí)間的。所以Message內(nèi)部弄了一個(gè)回收機(jī)制來(lái)管理Message。

Message的回收機(jī)制

和之前的文章一樣,既然是回收機(jī)制,那么自然會(huì)想到的問(wèn)題就是,怎么回收,回收之后怎么利用?我們?cè)趯W(xué)習(xí)Handler的時(shí)候 ,Google推薦我們使用Message的靜態(tài)方法obtain來(lái)得到一個(gè)新的Message實(shí)例。所以我們可以確定的是,回收之后的Message就是通過(guò)obtain來(lái)獲取新的Message的。那我們來(lái)看看obtain的源碼。

我們能看到的Message有很多obtain的重載,但是都使用了下面的無(wú)參方法,所以我們只看這份代碼就可以了

public static Message obtain() {
    synchronized (sPoolSync) {
        // sPool 就是Message的緩存池啦
        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();
}

obtain方法不難,而且代碼很多,很容易理解吧。所以我們繼續(xù)尋找Message是在哪里被回收的。我找到了下面的兩個(gè)方法:

/**
 * Return a Message instance to the global pool.
 * <p>
 * You MUST NOT touch the Message after calling this function because it has
 * effectively been freed.  It is an error to recycle a message that is currently
 * enqueued or that is in the process of being delivered to a Handler.
 * </p>
 */
public void recycle() {
    if (isInUse()) {
        if (gCheckRecycle) {
            throw new IllegalStateException("This message cannot be recycled because it "
                    + "is still in use.");
        }
        return;
    }
    recycleUnchecked();
}

/**
 * Recycles a Message that may be in-use.
 * Used internally by the MessageQueue and Looper when disposing of queued Messages.
 */
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++;
        }
    }
}

可以看到上面的方法就是我們需要的啦,回收Message的方法。在recycle()一開始就判斷了這個(gè)Message是否在使用,如果在使用就不回收啦。我之前一直以為Message的回收機(jī)制這些東西很高大上,其實(shí)在recycleUnchecked里面只是對(duì)Message的所有成員變量清空,初始化一遍而已啊。最后緩沖池的大小再加一。

Message的其他東西

Message的源碼不是很多,所以我還看了別的代碼:

public void copyFrom(Message o) {
    this.flags = o.flags & ~FLAGS_TO_CLEAR_ON_COPY_FROM;
    this.what = o.what;
    this.arg1 = o.arg1;
    this.arg2 = o.arg2;
    this.obj = o.obj;
    this.replyTo = o.replyTo;
    this.sendingUid = o.sendingUid;

    if (o.data != null) {
        this.data = (Bundle) o.data.clone();
    } else {
        this.data = null;
    }
}

這個(gè)方法是復(fù)制一個(gè)Message,除了Mesasge的data數(shù)據(jù)之外,其他都是淺拷貝的。我覺(jué)得這個(gè)在平時(shí)的開發(fā)中可能會(huì)寫錯(cuò)的,所有今天寫下來(lái)記錄一下

有一點(diǎn)我還是沒(méi)有想明白的,為什么Message要分同步和異步啊,有沒(méi)有dalao留個(gè)言說(shuō)說(shuō)什么回事啊

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

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

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