Handler Looper MessageQueue 原理淺析
說到Andorid線程間通信最常見的就是Handler偿荷,Handler的原理是個大廠面試必問,可見其重要程度唠椭。本文在這里從源碼角度淺析一下Handler跳纳,Looper和MessageQueue
1.從Looper開始
首先我們知道,Looper從字面理解就是輪子的意思贪嫂。
Looper的源碼很少寺庄,區(qū)區(qū)不到300行且大半都是注釋。我們直接上代碼:
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
可以看到Looper的構造方法里撩荣,new了一個MessageQueue铣揉,并且保存了當前的線程信息。
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));
}
...
//在ThreadLocal中的get方法
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}
prepare方法在調用時餐曹,首先去ThreadLocal中逛拱,以該線程為key,取該Looper的信息台猴。如果有的話則拋出異常朽合。證明每個線程只允許有一個Looper存在。
如果沒有從ThreadLocal中取出饱狂,則會將該new一個新的Looper存入ThreadLocal中曹步,并且該Looper與當前線程綁定。
prepare方法很簡潔休讳,接下來讓我們看看Looper循環(huán)的主方法: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;
...
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.recycleUnchecked();
}
}
可以看到首先這里有個死循環(huán)讲婚,防止程序運行結束終止退出。在循環(huán)入口會從MessageQueue中取出一條消息俊柔,就是queue.next()方法筹麸。
如果成功取到消息活合,則會調用msg.target.dispatchMessage(msg)。
這里的msg.target就是handler物赶,稍后會有該部分的源碼白指。
好了,以上就是Looper的主要源碼酵紫,代碼量十分少告嘲,很容易理解。
接下來要看看MessageQueue
2.MessageQueue
剛剛我們看到Looper的loop()方法調用了queue.next()奖地,該段代碼起到阻塞作用橄唬。所以我們從MessageQueue的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 (;;) {
...
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;
}
...
}
...
}
}
這段代碼信息量比較大鹉动,不過拆分來看轧坎,我們可以確定MessageQueue.next()方法調用的恰恰是Message的next變量賦值,Message是一個單鏈表結構泽示,其中next屬性指向下一個Message的地址缸血。
由此得知,當mMessages有值的時候械筛,Looper才會調用到msg.target.dispatchMessage()捎泻。
那么什么時候對mMessages進行賦值呢?
答案就在下一個非常重要的方法里:enqueueMessage()
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 {
// 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;
}
Message由enqueueMessage()方法傳入埋哟,在其內部對mMessages進行賦值笆豁。進而轉入next()方法跳出循環(huán),并通知Looper()調用msg.target.dispatchMessage()赤赊。
多說一嘴闯狱,大家可能對這里面這個msg.when參數(shù)比較好奇,這個when參數(shù)就是我們再調用Handler.sendMessageDelay()方法時抛计,傳入的delay時長哄孤。具體時間循環(huán)計算方法可見next(),這里不做過多解釋吹截。
貌似整體流程清晰了瘦陈,誰調用這個MessageQueue的enqueueMessage方法,誰就能將一條Message發(fā)送給Looper波俄,進而發(fā)送給Handler的dispatch()方法晨逝。
3.Handler
答案當然是Handler調用的MessageQueue的enqueueMessage()方法。
public final boolean sendMessageDelayed(Message msg, long delayMillis)
{
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
這段代碼僅僅是舉個例子懦铺,所有的Handler的post方法以及sendMessage方法捉貌,最終都會調用到sendMessageAtTime()。
接下來看看sendMessageAtTime():
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);
}
sendeMessageAtTime()最終會調用Handler的enqueueMessage()。
結果可想而知昏翰,Hanlder的enqueueMessage()必然會調用到MessageQueue的enqueueMessage()方法:
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
果然苍匆,最后我們看看Handler的dispathMessage()方法:
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
整個一條閉環(huán)流程清晰可見,最終msg通過了dispatchMessage()的分發(fā)棚菊,來到了我們常見的hanldeMessage()里面。
最后為了方便大家理解叔汁,我用一張圖來演示一下整個調用流程: