以下內(nèi)容整理自互聯(lián)網(wǎng)跌宛,僅用于個(gè)人學(xué)習(xí)
Android的線程間通信就靠Handler、Looper积仗、Message疆拘、MessageQueue。
1. Looper
先來(lái)看看looper.prepare()這個(gè)方法寂曹,它的作用是確保每個(gè)線程只有一個(gè)Looper哎迄。
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
//在當(dāng)前線程綁定一個(gè)Looper
sThreadLocal.set(new Looper(quitAllowed));
}
以上代碼只做了兩件事情: 1. 判斷當(dāng)前線程有木有Looper,如果有則拋出異常隆圆。 2. 如果沒(méi)有的話(huà)漱挚,那么就設(shè)置一個(gè)新的Looper到當(dāng)前線程。
上述代碼中調(diào)用的Looper的構(gòu)造函數(shù)渺氧,接下來(lái)看看構(gòu)造函數(shù)
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mRun = true;
mThread = Thread.currentThread();
}
Looper在構(gòu)造函數(shù)里干了兩件事情: 1. 將線程對(duì)象指向了創(chuàng)建Looper的線程旨涝。 2. 創(chuàng)建了一個(gè)新的MessageQueue。
再來(lái)看看looper.loop()方法
public static void loop() {
final Looper me = myLooper();//獲得當(dāng)前線程綁定的Looper
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;//獲得與Looper綁定的MessageQueue
// 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();
//進(jìn)入死循環(huán)侣背,不斷地去取對(duì)象白华,分發(fā)對(duì)象到Handler中消費(fèi)
for (;;) {
Message msg = queue.next(); // 不斷的取下一個(gè)Message對(duì)象,在這里可能會(huì)造成堵塞贩耐。
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);
}
//在這里弧腥,開(kāi)始分發(fā)Message
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);
}
//當(dāng)分發(fā)完Message之后,當(dāng)然要標(biāo)記將該Message標(biāo)記為 *正在使用* 啦
msg.recycleUnchecked();
}
}
分析了上面的源代碼潮太,我們可以意識(shí)到管搪,最重要的方法是:
- queue.next()
- msg.target.dispatchMessage(msg)
- msg.recycleUnchecked()
其實(shí)Looper中最重要的部分都是由Message、MessageQueue組成的消别!這段最重要的代碼中涉及到了四個(gè)對(duì)象,他們與彼此的關(guān)系如下:
- MessageQueue:裝食物的容器
- Message:被裝的食物
- Handler(msg.target實(shí)際上就是Handler):食物的消費(fèi)者
- Looper:負(fù)責(zé)分發(fā)食物的人
looper函數(shù)就是不斷從消息隊(duì)列里面取消息抛蚤,如果隊(duì)列里面沒(méi)有消息,就阻塞寻狂,直到有消息岁经,則把這個(gè)消息取出,并調(diào)用Hander的dispatchMessage函數(shù)蛇券。
2. Handler
先來(lái)分析Handler的構(gòu)造函數(shù)
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());
}
}
//獲取與創(chuàng)建Handler線程綁定的Looper
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
//獲取與Looper綁定的MessageQueue
//因?yàn)橐粋€(gè)Looper就只有一個(gè)MessageQueue缀壤,也就是與當(dāng)前線程綁定的MessageQueue
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
Handler通過(guò)獲取當(dāng)前線程的Looper對(duì)象樊拓,通過(guò)Looper對(duì)象來(lái)獲取并保存了消息隊(duì)列。Handler為什么要獲取消息隊(duì)列的引用呢塘慕?主要是筋夏,Handler需要把消息“塞”進(jìn)消息隊(duì)列里面,因此必須得有消息隊(duì)列引用图呢。
發(fā)送消息的sendMessage函數(shù)
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) {
//引用Handler中的MessageQueue
//這個(gè)MessageQueue就是創(chuàng)建Looper時(shí)被創(chuàng)建的MessageQueue
MessageQueue queue = mQueue;
if (queue == null) {
RuntimeException e = new RuntimeException(
this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
return false;
}
//將新來(lái)的Message加入到MessageQueue中
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);
}
從上面代碼看出条篷,sendMessage其本質(zhì)就是對(duì)應(yīng)一個(gè)消息入隊(duì)的過(guò)程。
前面我們知道蛤织,Looper從隊(duì)列里面取出消息后赴叹,調(diào)用Handler的dispatchMessage函數(shù)
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
可以看到,其實(shí)就是調(diào)用了handleMessage函數(shù)指蚜,而我們平時(shí)定義Handler對(duì)象時(shí)就是重寫(xiě)這個(gè)函數(shù)乞巧,因此取出消息后,會(huì)調(diào)用我們定義的handleMessage摊鸡。
總結(jié)
當(dāng)我們調(diào)用handler.sendMessage(msg)方法發(fā)送一個(gè)Message時(shí)绽媒,實(shí)際上這個(gè)Message是發(fā)送到與當(dāng)前線程綁定的一個(gè)MessageQueue中,然后與當(dāng)前線程綁定的Looper將會(huì)不斷的從MessageQueue中取出新的Message免猾,調(diào)用msg.target.dispathMessage(msg)方法將消息分發(fā)到與Message綁定的handler.handleMessage()方法中是辕。
一個(gè)Thread對(duì)應(yīng)多個(gè)Handler,一個(gè)Thread對(duì)應(yīng)一個(gè)Looper和MessageQueue掸刊,Handler與Thread共享Looper和MessageQueue免糕。 Message只是消息的載體,將會(huì)被發(fā)送到與線程綁定的唯一的MessageQueue中忧侧,并且被與線程綁定的唯一的Looper分發(fā)石窑,被與其自身綁定的Handler消費(fèi)。