先来看一段代码:
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 sThreadLocal = new ThreadLocal();//获取当前线程私有的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 - 原创文章,转载请注明出处


更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. Android(安卓)studio 工具中的“Attach Debugger to Android(安
  5. Android(安卓)-- Wifi连接流程分析
  6. Android防止暴力(多次)点击 - 代码已封装可直接使用
  7. Android缩放动画
  8. Android实现点击两次返回键退出
  9. Android(安卓)SDK下载和更新失败的解决方法

随机推荐

  1. 如何用Cdont+ASP发送带附件的html格式邮件
  2. 为TD设置css类
  3. Python脚本利用openoffice将office文档转
  4. iPad / iPhone请停止解析我网站上的电话
  5. 为什么我不能读取我的cookie值?
  6. Asp.net MVC 解决:CS0308: 非泛型 方法“
  7. 为什么ng-hide和ng-show不工作?
  8. HTML 5 就是 Web Application
  9. CSS文件:SyntaxError:期望表达式,得到'。
  10. 如何让这段插入的innerHTML 里动态赋予的