以前我有篇文章里面写到了Android的消息机制,Handler发送消息的一些原理。连接以下:async
从Handler.post(Runnable r)再一次梳理Android的消息机制(以及handler的内存泄露)ide
在消息机制里面,有一个很是重要的东西,那就是Looper,Looper的做用主要是从消息队列里面取出消息交给Handler处理,不过不只限于此,在这里面还有不少东西值得咱们去源码看一看:函数
要在一个线程里面处理消息,代码以下:oop
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(); }
首先就必需要先调用Looper.prepare(),那这个方法作了些什么呢:post
public static void prepare() { prepare(true); } 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)); }
代码其实只有关键性的一句,就是sThreadLocal.set(new Looper(quitAllowed)),首先来看看sThreadLocalui
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
ThreadLocal:表明了一个线程局部的变量,每条线程都只能看到本身的值,并不会意识到其它的线程中也存在该变量。
在这里ThreadLocal的做用是保证了每一个线程都有各自的Looperthis
上面的判断也说明了一个问题:一个线程只能有一个Looperspa
接下来看看建立Looper实例的方法new Looper(quitAllowed):.net
final MessageQueue mQueue; final Thread mThread; private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread(); }
在构造方法里,初始化了MessageQueue和表明当前线程的属性mThread,关于MessageQueue能够看看文章开头的连接,里面有详细的代码解析,这里就不赘述了。线程
调用Looper.prepare()其实就是利用ThreadLocal为当前的线程建立了一个独立的Looper,这其中包含了一个消息队列
在为当前线程建立了Looper以后,就能够建立Handler来处理消息了,这里能够解决咱们一个疑问:
Handler是怎么跟Looper关联上的?
//全局变量 final Looper mLooper; final MessageQueue mQueue; 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; }
在Handler中有两个全局变量mLooper和mQueue表明当前Handler关联的Looper和消息队列,并在构造函数中进行了初始化,重要的就是调用了:Looper.myLooper():
public static @Nullable Looper myLooper() { return sThreadLocal.get(); }
其实仍是调用的线程局部变量sThreadLocal,获取当前线程的Looper,这里须要注意的是,若是当前线程没有关联的Looper,这个方法会返回null。
这个方法也在以前的文章里讲到过,核心就是一个死循环,从MessageQueue里面取消息出来交给Handler来处理。
看了源码以后,咱们就知道了为啥在线程中须要处理消息,必需要通过以上三个步骤,且顺序不可更改
1.Looper.prepare():为当前线程准备消息队列2.Handler默认构造方法跟当前线程中的Looper产生关联
3.Looper.loop()开启循环取消息
这个问题在刚才已经探讨了,只能有一个,否则调用Looper.prepare()会抛出运行时异常,提示“Only one Looper may be created per thread”
能够建立无数个Handler,可是他们使用的消息队列都是同一个,也就是同一个Looper
这个问题就要来到Handler的 sendMessage方法里面了,具体的流程这里不详说了,最后来到了这个方法
Handler.enqueueMessage
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); }
能够看到这一句msg.target = this;,这里就是将当前的Handler赋值给Message对象,这样在处理消息的时候经过msg.target就能够区分开不一样的Handler了。处理的方法在Looper.loop中:
Looper.loop()
... msg.target.dispatchMessage(msg); ...
顺便提一句,在Message的obtain的各类重载方法里面也有对target的赋值