Thread thread = new Thread() { public void run() { //子线程中发送消息给主线程 Message msg = new Message(); msg.what = 200; msg.obj = param; msg.arg1 = 3; handler.sendMessage(msg); }; }; Handler handler = new Handler() { public void handleMessage(Message msg) { //主线程接收到消息,更新UI }; };
这段代码熟悉吗?
在Android中,有两种线程:主线程(UI线程,不允许做耗时的操作)和子线程(非UI线程,不允许操作界面元素)。而Handler是实现子线程与UI线程之间通讯的桥梁。那到底这是怎么实现的呢?带着这个疑问,我们去Android源码中寻找答案吧!
先看一下Handler的构造方法:
public Handler() { this(null, false); } public Handler(Callback callback, boolean async) { 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; }
构造方法中初始化了以下的两个变量:
final MessageQueue mQueue; //消息队列(链表结构,下面会分析到) final Looper mLooper; //可理解为消息处理器而MessageQueue是Looper的成员属性。
下面跟踪Handler的sendMessage(msg)方法进去,可看到这个方法:
public boolean sendMessageAtTime(Message msg, long uptimeMillis) { MessageQueue queue = mQueue; if (queue == null) {} return enqueueMessage(queue, msg, uptimeMillis); } private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { //注意这一行,将Handler自已赋值给了Message的target属性,下面的析中会用到 msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); }
我们再看一下MessageQueue的enqueueMessage(msg,when)方法的实现:
boolean enqueueMessage(Message msg, long when) { synchronized (this) { 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; }
上面的代码中,我们可得知:
消息在MessageQueue类中是按链表的方式存储的,MessageQueue类中以成员属性变量mMessages记录了一个排在最前面的消息 ,每当有新消息插入时,根据时间(when)的先后顺序重新排列,时间最早的在最前面。
我们在Looper类的注释中,找到下面这样的示例代码:
class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here } }; Looper.loop(); } }
这个示例可在直接在我们平常开发中使用。注意到,这里也新建了一个Handler实例,就像我们常在Activity中新建一样。我们知道,Activity是运行在UI线程中的,而UI线程也是一个线程,所以,我们猜想,Activity中新建Handler实例跟在这个示例的线程中新建是一样的效果。
我们还发现示例中,Handler的创建的前后分别调用了两个静态方法:
Looper.prepare(); Looper.loop();这里面大有学问,在继续往下分析之前,我们再大胆猜测UI线程加载Activity的过程的前后也调用了这两个方法。
public static void prepare() { prepare(true); } //设置当前线程私有的Looper对象 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)); } //定义当前线程私有的Looper对象 static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); //获取当前线程私有的Looper对象 public static Looper myLooper() { return sThreadLocal.get(); }
prepare()方法实际上是为当前线程创建了自己私有的Looper对象,连同它的属性MessageQueue消息队列也是当前线程私有的。其他线程可以有他们自己的那个Looper,但不可以访问当前线程的Looper。
Looper.loop();用于在线程中不断地处理MessageQueue消息队列中的消息,看一下它的实现代码:
/** * Run the message queue in this thread. Be sure to call * {@link #quit()} to end the 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; } msg.target.dispatchMessage(msg); msg.recycle(); } }
方法实现中,用一个死循环不断地提取MessageQueue队列中的消息,然后调用消息的Target去分发这个消息。Target是什么? 正是上面分析到的Handler的enqueueMessage(...)方法中所设置的Handler自已本身。
再看一下Handler类的dispatchMessage(msg)方法:
/** * Handle system messages here. */ public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
这里调用到了我们非常熟悉的handleMessage(msg)方法了。
下面总结一下:
在多线程的环境中,主线程和子线程之间交互是通过一个链表结构的消息队列(MessageQueue),子线程只管往里面放入消息(Message),消息是按时间的先后顺序排列的,主线程用一个消息处理器(Looper)不断地逐个逐个地处理掉消息。
@容新华技术博客 - http://blog.csdn.net/rongxinhua - 原创文章,转载请注明出处