学习android的一大乐趣是可以通过源码学习google大牛们的设计思想。android源码中包含了大量的设计模式,除此以外,android sdk还精心为我们设计了各种helper类,对于和我一样渴望水平得到进阶的人来说,都太值得一读了。这不,前几天为了了解android的消息处理机制,我看了
Looper,Handler,Message
这几个类的源码,结果又一次被googler的设计震撼了,特与大家分享。
android的消息处理有三个核心类:Looper,Handler和Message。其实还有一个Message Queue(消息队列),但是MQ被封装到Looper里面了,我们不会直接与MQ打交道,因此我没将其作为核心类。下面一一介绍:
线程的魔法师 Looper
Looper的字面意思是“循环者”,它被设计用来使一个普通线程变成Looper线程。所谓Looper线程就是循环工作的线程。在程序开发中(尤其是GUI开发中),我们经常会需要一个线程不断循环,一旦有新任务则执行,执行完继续等待下一个任务,这就是Looper线程。使用Looper类创建Looper线程很简单:
01 | public class LooperThread extends Thread { |
通过上面两行核心代码,你的线程就升级为Looper线程了!!!是不是很神奇?让我们放慢镜头,看看这两行代码各自做了什么。
1)Looper.prepare()
![](https://i-blog.csdnimg.cn/blog_migrate/1faff953bea2b487a1273638545da992.png)
通过上图可以看到,现在你的线程中有一个Looper对象,它的内部维护了一个消息队列MQ。注意,一个Thread只能有一个Looper对象,为什么呢?咱们来看源码。
03 | private static final ThreadLocal sThreadLocal = new ThreadLocal(); |
05 | final MessageQueue mQueue; |
12 | mQueue = new MessageQueue(); |
14 | mThread = Thread.currentThread(); |
18 | public static final void prepare() { |
19 | if (sThreadLocal.get() != null ) { |
21 | throw new RuntimeException( "Only one Looper may be created per thread" ); |
23 | sThreadLocal.set( new Looper()); |
通过源码,prepare()背后的工作方式一目了然,其核心就是将looper对象定义为ThreadLocal。如果你还不清楚什么是ThreadLocal,请参考《理解ThreadLocal》。
2)Looper.loop()
![](https://i-blog.csdnimg.cn/blog_migrate/849603c0297f20b2edcc5847a6e763d4.png)
调用loop方法后,Looper线程就开始真正工作了,它不断从自己的MQ中取出队头的消息(也叫任务)执行。其源码分析如下:
01 | public static final void loop() { |
02 | Looper me = myLooper(); |
03 | MessageQueue queue = me.mQueue; |
06 | Binder.clearCallingIdentity(); |
07 | final long ident = Binder.clearCallingIdentity(); |
10 | Message msg = queue.next(); |
12 | if (msg.target == null ) { |
17 | if (me.mLogging!= null ) me.mLogging.println( |
18 | ">>>>> Dispatching to " + msg.target + " " |
19 | + msg.callback + ": " + msg.what |
22 | msg.target.dispatchMessage(msg); |
24 | if (me.mLogging!= null ) me.mLogging.println( |
25 | "<<<<< Finished to " + msg.target + " " |
29 | final long newIdent = Binder.clearCallingIdentity(); |
30 | if (ident != newIdent) { |
31 | Log.wtf( "Looper" , "Thread identity changed from 0x" |
32 | + Long.toHexString(ident) + " to 0x" |
33 | + Long.toHexString(newIdent) + " while dispatching to " |
34 | + msg.target.getClass().getName() + " " |
35 | + msg.callback + " what=" + msg.what); |
除了prepare()和loop()方法,Looper类还提供了一些有用的方法,比如
Looper.myLooper()得到当前线程looper对象:
public static final Looper myLooper() {
// 在任意线程调用Looper.myLooper()返回的都是那个线程的looper
return (Looper)sThreadLocal.get();
}
getThread()得到looper对象所属线程:
public Thread getThread() {
return mThread;
}
quit()方法结束looper循环:
public void quit() {
// 创建一个空的message,它的target为NULL,表示结束循环消息
Message msg = Message.obtain();
// 发出消息
mQueue.enqueueMessage(msg, 0);
}
到此为止,你应该对Looper有了基本的了解,总结几点:
1.每个线程有且最多只能有一个Looper对象,它是一个ThreadLocal
2.Looper内部有一个消息队列,loop()方法调用后线程开始不断从队列中取出消息执行
3.Looper使一个线程变成Looper线程。
那么,我们如何往MQ上添加消息呢?下面有请Handler!(掌声~~~)
异步处理大师 Handler
什么是handler?handler扮演了往MQ上添加消息和处理消息的角色(只处理由自己发出的消息),即通知MQ它要执行一个任务(sendMessage),并在loop到自己的时候执行该任务(handleMessage),整个过程是异步的。handler创建时会关联一个looper,默认的构造方法将关联当前线程的looper,不过这也是可以set的。默认的构造方法:
03 | final MessageQueue mQueue; |
05 | final Callback mCallback; |
10 | if (FIND_POTENTIAL_LEAKS) { |
11 | final Class<? extends Handler> klass = getClass(); |
12 | if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && |
13 | (klass.getModifiers() & Modifier.STATIC) == 0 ) { |
14 | Log.w(TAG, "The following Handler class should be static or leaks might occur: " + |
15 | klass.getCanonicalName()); |
19 | mLooper = Looper.myLooper(); |
22 | throw new RuntimeException( |
23 | "Can't create handler inside thread that has not called Looper.prepare()" ); |
26 | mQueue = mLooper.mQueue; |
下面我们就可以为之前的LooperThread类加入Handler:
01 | public class LooperThread extends Thread { |
02 | private Handler handler1; |
03 | private Handler handler2; |
11 | handler1 = new Handler(); |
12 | handler2 = new Handler(); |
加入handler后的效果如下图:
![](https://i-blog.csdnimg.cn/blog_migrate/e31578c7aee55edc15f8d5c4a54687d9.png)
可以看到,一个线程可以有多个Handler,但是只能有一个Looper!
Handler发送消息
有了handler之后,我们就可以使用 post(Runnable), postAtTime(Runnable, long),postDelayed(Runnable, long), sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long)和 sendMessageDelayed(Message, long)这些方法向MQ上发送消息了。光看这些API你可能会觉得handler能发两种消息,一种是Runnable对象,一种是message对象,这是直观的理解,但其实post发出的Runnable对象最后都被封装成message对象了,见源码:
02 | public final boolean post(Runnable r) |
05 | return sendMessageDelayed(getPostMessage(r), 0 ); |
08 | private final Message getPostMessage(Runnable r) { |
09 | Message m = Message.obtain(); |
14 | public boolean sendMessageAtTime(Message msg, long uptimeMillis) |
17 | MessageQueue queue = mQueue; |
20 | sent = queue.enqueueMessage(msg, uptimeMillis); |
23 | RuntimeException e = new RuntimeException( |
24 | this + " sendMessageAtTime() called with no mQueue" ); |
25 | Log.w( "Looper" , e.getMessage(), e); |
其他方法就不罗列了,总之通过handler发出的message有如下特点:
1.message.target为该handler对象,这确保了looper执行到该message时能找到处理它的handler,即loop()方法中的关键代码
msg.target.dispatchMessage(msg);
2.post发出的message,其callback为Runnable对象
Handler处理消息
说完了消息的发送,再来看下handler如何处理消息。消息的处理是通过核心方法dispatchMessage(Message msg)与钩子方法handleMessage(Message msg)完成的,见源码
02 | public void dispatchMessage(Message msg) { |
03 | if (msg.callback != null ) { |
08 | if (mCallback != null ) { |
10 | if (mCallback.handleMessage(msg)) { |
20 | private final void handleCallback(Message message) { |
21 | message.callback.run(); |
24 | public void handleMessage(Message msg) { |
可以看到,除了handleMessage(Message msg)和Runnable对象的run方法由开发者实现外(实现具体逻辑),handler的内部工作机制对开发者是透明的。这正是handler API设计的精妙之处!
Handler的用处
我在小标题中将handler描述为“异步处理大师”,这归功于Handler拥有下面两个重要的特点:
1.handler可以在任意线程发送消息,这些消息会被添加到关联的MQ上。
2.handler是在它关联的looper线程中处理消息的。
![](https://i-blog.csdnimg.cn/blog_migrate/6de34bd6cad60713137c4184d8b8a76b.png)
这就解决了android最经典的不能在其他非主线程中更新UI的问题。android的主线程也是一个looper线程(looper在android中运用很广),我们在其中创建的handler默认将关联主线程MQ。因此,利用handler的一个solution就是在activity中创建handler并将其引用传递给worker thread,worker thread执行完任务后使用handler发送消息通知activity更新UI。(过程如图)
![](https://i-blog.csdnimg.cn/blog_migrate/e04e07f61350a8d19939d45dcc5bad5b.png)
下面给出sample代码,仅供参考:
01 | public class TestDriverActivity extends Activity { |
02 | private TextView textview; |
05 | protected void onCreate(Bundle savedInstanceState) { |
06 | super .onCreate(savedInstanceState); |
07 | setContentView(R.layout.main); |
08 | textview = (TextView) findViewById(R.id.textview); |
10 | Thread workerThread = new Thread( new SampleTask( new MyHandler())); |
14 | public void appendText(String msg) { |
15 | textview.setText(textview.getText() + "\n" + msg); |
18 | class MyHandler extends Handler { |
20 | public void handleMessage(Message msg) { |
21 | String result = msg.getData().getString( "message" ); |
30 | public class SampleTask implements Runnable { |
31 | private static final String TAG = SampleTask. class .getSimpleName(); |
34 | public SampleTask(Handler handler) { |
36 | this .handler = handler; |
44 | Message msg = prepareMessage( "task completed!" ); |
46 | handler.sendMessage(msg); |
47 | } catch (InterruptedException e) { |
48 | Log.d(TAG, "interrupted!" ); |
53 | private Message prepareMessage(String str) { |
54 | Message result = handler.obtainMessage(); |
55 | Bundle data = new Bundle(); |
56 | data.putString( "message" , str); |
当然,handler能做的远远不仅如此,由于它能post Runnable对象,它还能与Looper配合实现经典的Pipeline Thread(流水线线程)模式。请参考此文《Android Guts: Intro to Loopers and Handlers》
封装任务 Message
在整个消息处理机制中,message又叫task,封装了任务携带的信息和处理该任务的handler。message的用法比较简单,这里不做总结了。但是有这么几点需要注意(待补充):
1.尽管Message有public的默认构造方法,但是你应该通过Message.obtain()来从消息池中获得空消息对象,以节省资源。
2.如果你的message只需要携带简单的int信息,请优先使用Message.arg1和Message.arg2来传递信息,这比用Bundle更省内存
3.擅用message.what来标识信息,以便用不同方式处理message。