網(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ǔ)上另外寫一篇更深入的。
總覺得寫來寫去還是寫的不夠好蕉世,不曉得你們能不能看懂= =
如果有寫的不正確的地方蔼紧,就趕快告訴我啊哈哈哈