Android Handler消息處理機(jī)制(菜鳥(niǎo)篇)

網(wǎng)上有很多關(guān)于Handler,Looper,Message的分析。
但是很多都是大概的講了一個(gè)思路,并且只是挑了幾個(gè)重點(diǎn)做了比較詳細(xì)的敘述,只要不是自己本來(lái)就了解的很清楚,看完以后一般都有點(diǎn)云里霧里。
比如說(shuō)提到MessageQueue.next()這個(gè)方法,由native方法阻塞獲取Message和監(jiān)聽(tīng)event觸發(fā)什么的,沒(méi)看過(guò)源碼的連MessageQueue.next()在Looper里面調(diào)用都不知道,要怎么跟上你的思路 ?。?!


這篇文章僅僅寫(xiě)一個(gè)Message的send到handle過(guò)程,不深入到native,不考慮線程同步等等問(wèn)題。
我覺(jué)得首先了解是怎么工作的,將整個(gè)流程熟悉了之后,再考慮底層的實(shí)現(xiàn)以及一些線程同步,數(shù)據(jù)儲(chǔ)存方式和異常發(fā)生的情況。


其實(shí)大概的原理就是,Handler發(fā)送Message到MessageQueue,Looper從MessageQueue中取出Message執(zhí)行。


另外這些類(lèi)的關(guān)系,一個(gè)線程最多只有一個(gè)Looper,一個(gè)Looper持有一個(gè)MessageQueue,一個(gè)Looper可以對(duì)應(yīng)多個(gè)Handler。可以下面列出這些類(lèi)和在這片文章里需要用到的成員。

Class Fields
Message 1.target(Handler)
是一個(gè)Handler
標(biāo)記Message屬于哪個(gè)Handler
2.when(long)
什么時(shí)候發(fā)送這個(gè)messag
3.next(Message)
可以作為一個(gè)List
4.另外就是一些數(shù)據(jù)了
MessageQueue Message
用Message.next組成List
Looper 1.ThreadLocal
可以說(shuō)是用來(lái)保存
某個(gè)線程的Looper
2.MessageQueue
Looper持有的MessageQueue
Handler MessageQueue
通過(guò)其所屬的Looper
得到MessageQueue

好了,下面是重頭戲(有點(diǎn)啰嗦,見(jiàn)諒),我們知道UI線程自動(dòng)幫你創(chuàng)建Looper

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

我們進(jìn)去看prepare(false),忽略那個(gè)boolean值

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

首先看sThreadLocal這個(gè)東西,就是保存當(dāng)前線程的Looper用的ThreadLocal

static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();

然后進(jìn)去看get方法

public T get() {    
    // Optimized for the fast path.    
    Thread currentThread = Thread.currentThread();    
    Values values = values(currentThread);    
    if (values != null) {        
        Object[] table = values.table;        
        int index = hash & values.mask;        
        if (this.reference == table[index]) {            
            return (T) table[index + 1];        
        }    
    } else {        
        values = initializeValues(currentThread);    
    }    
    return (T) values.getAfterMiss(this);
}

Values values(Thread current) {    
    return current.localValues;
}

可以看到Values是通過(guò)Thread拿到的,進(jìn)到Thread.class可以看到類(lèi)型為T(mén)hreadLocal.Values的值

/** * Normal thread local values. */
ThreadLocal.Values localValues;

我們暫時(shí)先不深入下去了解是怎么存儲(chǔ)的,只要知道Looper是通過(guò)ThreadLocal.Values來(lái)取的,當(dāng)然也是通過(guò)這個(gè)來(lái)存的。
我們回到prepare()方法,程序剛啟動(dòng),所以可以確定sThreadLocal.get() = null,所以會(huì)執(zhí)行sThreadLocal.set(new Looper(quitAllowed)),也就是給UI的線程新建一個(gè)Looper。
然后我們繼續(xù)看Looper的構(gòu)造器

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

我們可以看到Looper在new的時(shí)候會(huì)持有一個(gè)MessageQueue,現(xiàn)在Looper,MessageQueue已經(jīng)準(zhǔn)備就緒了。
之后,只要調(diào)用Looper.loop()就能讓Looper循環(huán)去取Message,我們來(lái)看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();    
    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        
        Printer logging = me.mLogging;        
        if (logging != null) {            
            logging.println(">>>>> Dispatching to " + msg.target + " " +msg.callback + ": " + msg.what);        
        }        
        msg.target.dispatchMessage(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();    
    }
}

我們先來(lái)看開(kāi)頭

final Looper me = myLooper();

中的myLooper()

/** * Return the Looper object associated with the current thread.  Returns * null if the calling thread is not associated with a Looper. */
public static @Nullable Looper myLooper() {    
    return sThreadLocal.get();
}

我們可以看到當(dāng)前線程的Looper就是通過(guò)sThreadLocal.get(),也就是Thread.currentThread().localValues這個(gè)值來(lái)拿的,現(xiàn)在我們已經(jīng)拿到的當(dāng)前線程的Looper。
然后我們?cè)倏催@句

final MessageQueue queue = me.mQueue;

我們之前看到Looper的構(gòu)造器里new了一個(gè)MessageQueue,所以我們現(xiàn)在得到了當(dāng)前線程的Looper所持有的MessageQueue,之后在for里面不斷從MessageQueue里面取Message

Message msg = queue.next(); 

其中queue.next()里面有很多native方法,這里先不深入,另外queue.next()這個(gè)方法會(huì)在沒(méi)有message的時(shí)候阻塞,拿到Message之后,看下面這句

msg.target.dispatchMessage(msg);

之前說(shuō)到Message里面有一個(gè)target是Handler類(lèi)型,也就是發(fā)送這個(gè)Message的Handler。
然后進(jìn)Handler看dispatchMessage方法

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

我們可以看到handleMessage()這個(gè)方法,這也就是Looper從MessageQueue里拿Message給它自己的Handler處理的過(guò)程。
現(xiàn)在整個(gè)消息循環(huán)系統(tǒng)已經(jīng)建成了,我們只需要同Handler發(fā)Message即可。
我們先新建一個(gè)Handler

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

我們看這兩句

mLooper = Looper.myLooper();
mQueue = mLooper.mQueue;

Handler持有了當(dāng)前線程的Looper的MessageQueue,現(xiàn)在我們發(fā)消息handler.sendMessage();

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,給message的target設(shè)置為當(dāng)前的handler,這樣就可以調(diào)用指定handler的dispatchMessage方法了,就像上面寫(xiě)到的msg.target.dispatchMessage(msg)。
然后用在Handler進(jìn)行new的時(shí)候持有的MessageQueue執(zhí)行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;
}

主要看中間這段代碼,其實(shí)就是把一個(gè)Message按照when(執(zhí)行的時(shí)間)插入到一個(gè)Message鏈表,和指針的寫(xiě)法是一樣的(這部分看不懂的自覺(jué)面壁思過(guò))
最后扯一丟丟native的方法,就是最后nativeWake(mPtr)這個(gè)方法,簡(jiǎn)單點(diǎn)說(shuō)就是通知有新的Message,然后Looper.loop()里面Message msg = queue.next();這句本來(lái)阻塞的方法就能拿到最新的Message。


這篇文章主要就是講handler.sendMessage()之后到handler.handleMessage()的過(guò)程,關(guān)于native,線程同步,以及其他情況的考慮,之后會(huì)在這篇的基礎(chǔ)上另外寫(xiě)一篇更深入的。
總覺(jué)得寫(xiě)來(lái)寫(xiě)去還是寫(xiě)的不夠好,不曉得你們能不能看懂= =
如果有寫(xiě)的不正確的地方,就趕快告訴我啊哈哈哈

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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