寫在前面:
如果你在看本文之前沒有對Android消息機制作過了解,可能會比較吃力芳来,關于源碼,不需要全部看懂猜拾,能看懂其中關鍵的幾句代碼就行了即舌。如果在閱讀過程中感到吃力,請直接跳到結尾部分看概述挎袜。
最近在看《Android開發(fā)藝術探索》顽聂,感覺真的是一本好書,恩盯仪,讓我非常有讀完欲望的一本書紊搪。話不多說,分享一下我的讀書收獲全景。
Android中的耗時操作需要在子線程中完成耀石,當這些操作完成后可能會需要對UI進行相應的更新。但是Android中的UI不是線程安全的蚪燕,在多線程中并發(fā)訪問可能會導致UI控件處于不可預期的狀態(tài)娶牌。Google沒有用上鎖機制來解決這個問題奔浅,而是讓開發(fā)者通過Handler切換線程來達到更新UI的目的馆纳。Handler是Android消息機制的上層接口,想要解析Android消息機制還需要了解Looper和消息隊列(MessageQueue)汹桦。
在具體的了解Handler鲁驶、Looper和MessageQueue之前,先讓我們把我們平時切換線程的流程過一遍舞骆。首先我們在主線程中創(chuàng)建Handler钥弯,然后在子線程中通過handler的sendMessage方法將攜帶子線程操作結果的數據傳出,通過handler的handleMessage方法進行UI的更新督禽,代碼如下:
public class MainActivity extends AppCompatActivity {
//注1
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
tvContent.setText((String)msg.obj);
}
};
private TextView tvContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvContent = (TextView) findViewById(R.id.tv_content);
new Thread(new Runnable() {
@Override
public void run() {
//...一些耗時操作
//耗時操作的結果
String result = "我是結果";
Message msg = Message.obtain();
msg.obj = result;
handler.sendMessage(msg);
}
}).start();
}
}
注1:這篇算是挺久之前的文了吧脆霎,現在看來有一個比較致命的東西,private Handler handler;這個東西是一個內部類狈惫,在Java中內部類都會隱含的持有一個外部類的引用睛蛛,一般來說這個外部類的引用就是Activity。如果因為Handler持有了Activity的引用而導致Activity無法被銷毀胧谈,則會導致內存泄露忆肾,解決方法就是用static修飾,static修飾的內部類不會持有外部類的引用菱肖。但是很快你就會發(fā)現因為他是靜態(tài)的客冈,所以無法使用外部類的成員了,這對我們更新UI又是個阻礙稳强,你可以考慮采用SoftReference的技術场仲,在Handler內部持有一個activity的軟引用和悦,在軟引用不為空的情況下通過這個弱引用去訪問外部類的成員。
因為代碼比較簡單渠缕,就不上結果圖了摹闽。接下來解析一下以上簡單的代碼。
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
tvContent.setText((String)msg.obj);
}
};
首先是構造一個Handler對象褐健,用的是默認的構造方法付鹿,看一下源碼是如何實例化的
public Handler() {
this(null, false);
}
再追蹤一下源碼,發(fā)現最終是用的如下的構造方法
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得到了一個Looper和一個“隊列”。查看myLooper方法的注釋
/**
* Return the Looper object associated with the current thread. Returns
* null if the calling thread is not associated with a Looper.
*/
返回的是當前線程的Looper谁不,如果當前線程沒有Looper就返回null坐梯。所以在Handler的構造方法中,會有
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
所以在沒有Looper的線程中創(chuàng)建Handler對象會拋以上異常刹帕。我們可以為沒有Looper的線程創(chuàng)建一個Looper不過這里先不談吵血。看完了如何創(chuàng)建Handler之后偷溺,繼續(xù)我們的流程蹋辅,在子線程中我們使用handler的sendMessage()方法發(fā)送了一個Message對象,讓我們看看這背后隱藏了怎樣的py交……挫掏,不侦另,怎樣的操作。
public final boolean sendMessage(Message msg)
{
return sendMessageDelayed(msg, 0);
}
通過追蹤源碼發(fā)現最終執(zhí)行了如下代碼:
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);
}
最后一句代碼是向queue中插入msg的意思尉共,也就是說handler.sendMessage()這個方法所執(zhí)行的操作就是向消息隊列插入了一條message褒傅,那么這條消息又是經歷了怎樣的輾轉才切換到了當前線程呢?顯然這其中有Looper的參與,不過在了解Looper之前袄友,需要先了解一下MessageQueue殿托。
MessageQueue#
MessageQueue雖然叫消息隊列,但實際上他是通過一個單鏈表的數據結構來維護消息列表剧蚣。至于為什么采用單鏈表的數據結構支竹,因為MessageQueue主要操作是插入和讀取,而讀取包含著刪除操作券敌,而單鏈表在插入和刪除上比較有優(yōu)勢唾戚。關于MessageQueue還需要知道enqueueMessage()是插入操作,next()是讀取并且刪除的操作待诅。需要了解的暫時就這么多了叹坦。
Looper
Looper會不停地從MessageQueue中查看是否有新消息,如果有新消息就會立刻處理卑雁,否則就一直阻塞在那里募书。Looper最重要的方法便是loop()方法绪囱,讓我們看一下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();
}
}
如果MessageQueue的next方法返回了新的消息,Looper就會處理這條消息
msg.target.dispatchMessage(msg);
msg.target就是發(fā)送這條消息的Handler對象,這樣Handler發(fā)送的消息最終又交給它的dispatchMessage方法來處理了。查看dispatchMessage方法源碼會發(fā)現幢码,我們之前使用的Handler的構造方法和得到Message對象的方法,最終會導致dispatchMessage方法調用handleMessage方法齿椅。
總結與拓展
總的來說,我們平常使用Handler所經歷的流程就是:
1.使用當前線程的Looper對象創(chuàng)建Handler启泣。
2.handler.sendMessage()向Looper中的消息隊列插入消息涣脚。
3.Looper通過loop方法獲取消息隊列的新消息,通過msg.target(發(fā)送消息的Handler)調用dispatchMessage方法處理消息寥茫。而該方法在loop方法中被調用遣蚀,loop被當前線程Looper調用,所以該消息被切換到當前線程中執(zhí)行纱耻。
在了解了Android的消息機制之后芭梯,我們可以嘗試使用Handler和Looper實現兩個子線程之間的消息傳遞。通過new Thread()創(chuàng)建的線程并沒有Looper弄喘,為他創(chuàng)建一個Looper就可以使用Handler了玖喘。Looper可以通過prepare方法來創(chuàng)建,好了說明就到這限次,直接上代碼芒涡!
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
private Handler mhandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread1 thread1 = new Thread1();
thread1.setName("Thread#1");
thread1.start();
mhandler = thread1.getHandler();
while(mhandler == null){
mhandler = thread1.getHandler();
}
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
Message msg = Message.obtain();
msg.obj = System.currentTimeMillis();
mhandler.sendMessage(msg);
}
}
}, "Thread#2").start();
}
class Thread1 extends Thread {
private Handler mhandler;
public Handler getHandler() {
return mhandler;
}
@Override
public void run() {
//為該線程創(chuàng)建Looper
Looper.prepare();
//初始化handler
mhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i(TAG, Thread.currentThread().getName() + " the message is:" + msg.obj);
}
};
Looper.loop();
}
}
}
很明顯,Thread#2成功地將消息傳遞到了Thread#1的handler中。其實關于Looper.prepare()也值得深究卖漫,涉及到了ThreadLocal,不過我這里就不管了赠群。
最后再安排一下今后一段時間內要做的事羊始,從第一篇簡書文章到這第二篇,中間隔了很久了查描。倒是沒有偷懶突委,一直在敲代碼,不過最近算是把一直想做的東西做了冬三,是時候來一波學習總結了匀油。
資料來源:《Android開發(fā)藝術探索》