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

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


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


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


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

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
可以說是用來保存
某個(gè)線程的Looper
2.MessageQueue
Looper持有的MessageQueue
Handler MessageQueue
通過其所屬的Looper
得到MessageQueue

好了,下面是重頭戲(有點(diǎn)啰嗦似扔,見諒)吨些,我們知道UI線程自動幫你創(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是通過Thread拿到的豪墅,進(jìn)到Thread.class可以看到類型為ThreadLocal.Values的值

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

我們暫時(shí)先不深入下去了解是怎么存儲的,只要知道Looper是通過ThreadLocal.Values來取的黔寇,當(dāng)然也是通過這個(gè)來存的偶器。
我們回到prepare()方法,程序剛啟動,所以可以確定sThreadLocal.get() = null屏轰,所以會執(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í)候會持有一個(gè)MessageQueue亭枷,現(xiàn)在Looper袭艺,MessageQueue已經(jīng)準(zhǔn)備就緒了。
之后叨粘,只要調(diào)用Looper.loop()就能讓Looper循環(huán)去取Message猾编,我們來看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();    
    }
}

我們先來看開頭

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就是通過sThreadLocal.get(),也就是Thread.currentThread().localValues這個(gè)值來拿的升敲,現(xiàn)在我們已經(jīng)拿到的當(dāng)前線程的Looper答倡。
然后我們再看這句

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è)方法會在沒有message的時(shí)候阻塞港庄,拿到Message之后倔既,看下面這句

msg.target.dispatchMessage(msg);

之前說到Message里面有一個(gè)target是Handler類型,也就是發(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處理的過程。
現(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方法了,就像上面寫到的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鏈表缀踪,和指針的寫法是一樣的(這部分看不懂的自覺面壁思過)
最后扯一丟丟native的方法腺晾,就是最后nativeWake(mPtr)這個(gè)方法,簡單點(diǎn)說就是通知有新的Message辜贵,然后Looper.loop()里面Message msg = queue.next();這句本來阻塞的方法就能拿到最新的Message悯蝉。


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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市狠轻,隨后出現(xiàn)的幾起案子奸例,更是在濱河造成了極大的恐慌,老刑警劉巖向楼,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件查吊,死亡現(xiàn)場離奇詭異,居然都是意外死亡湖蜕,警方通過查閱死者的電腦和手機(jī)逻卖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來昭抒,“玉大人评也,你說我怎么就攤上這事∶鸱担” “怎么了盗迟?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長熙含。 經(jīng)常有香客問我诈乒,道長,這世上最難降的妖魔是什么婆芦? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮喂饥,結(jié)果婚禮上消约,老公的妹妹穿的比我還像新娘。我一直安慰自己或粮,他們只是感情好捞高,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著氢哮,像睡著了一般型檀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天皆看,我揣著相機(jī)與錄音背零,去河邊找鬼。 笑死毛雇,一個(gè)胖子當(dāng)著我的面吹牛倍啥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播虽缕,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼氮趋,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了剩胁?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤晾腔,失蹤者是張志新(化名)和其女友劉穎啊犬,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體剔应,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡语御,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年应闯,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片碉纺。...
    茶點(diǎn)故事閱讀 39,773評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖唬涧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情捧搞,我是刑警寧澤狮荔,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站晚树,受9級特大地震影響雅采,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜婚瓜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望愚铡。 院中可真熱鬧胡陪,春花似錦、人聲如沸柠座。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至鳄厌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間泪漂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工露筒, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留敌卓,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓瘪吏,卻偏偏與公主長得像蜗巧,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子幕屹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容