刚看源码的时候:“这TM写的是啥?那写的又TM是啥?” 研究明白了以后:“奥,原来就这点玩意儿啊,太简单了。”缓存
Looper的职责很单一,就是单纯的从MessageQueue中取出消息分发给消息对应的宿主Handler,所以它的代码很少(300行左右)。安全
Looper是线程独立的且每一个线程只能存在一个Looper。bash
Looper会根据本身的存活状况来建立和退出属于它本身的MessageQueue。markdown
上面的结论中提到了Looper是线程独立的且每一个线程只能存在一个Looper。因此构造Looper实例的方法相似于单例模式。隐藏构造方法,对外提供了两个指定的获取实例方法prepare()
和prepareMainLooper()
。ide
// 应用主线程(UI线程)Looper实例 private static Looper sMainLooper; // Worker线程Looper实例,用ThreadLocal保存的对象都是线程独立的 static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); // 与当前Looper对应的消息队列 final MessageQueue mQueue; // 当前Looper因此的线程 final Thread mThread; /** * 对外公开初始化方法 * * 在普通线程中初始化Looper调用此方法 */ public static void prepare() { // 初始化一个能够退出的Looper prepare(true); } /** * 对外公开初始化方法 * * 在应用主线程(UI线程)中初始化Looper调用此方法 */ public static void prepareMainLooper() { // 由于是主线程,初始化一个不容许退出的Looper prepare(false); synchronized (Looper.class) { // 若是sMainLooper不等于空说明已经建立过主线程Looper了,不该该重复建立 if (sMainLooper != null) { throw new IllegalStateException("The main Looper has already been prepared."); } sMainLooper = myLooper(); } } /** * 内部私有初始化方法 * @param quitAllowed 是否容许退出Looper */ private static void prepare(boolean quitAllowed) { // 每一个线程只能有一个Looper if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } // 保存实例 sThreadLocal.set(new Looper(quitAllowed)); } /** * 私有构造方法 * @param quitAllowed 是否容许退出Looper */ private Looper(boolean quitAllowed) { // 初始化MessageQueue mQueue = new MessageQueue(quitAllowed); // 获得当前线程实例 mThread = Thread.currentThread(); } 复制代码
真正建立Looper实例的构造方法中其实很简单,就是建立了对应的MessageQueue实例,而后获得当前线程,值得注意的是MessageQueue和线程实例都是被final
关键字修饰的,只能被赋值一次。oop
对外公开初始化方法prepareMainLooper()
是为应用主线程(UI线程)准备的,应用刚被建立就会调用该方法,因此咱们不应再去调用它。性能
开发者能够经过调用对外公开初始化方法prepare()
对本身的worker线程建立Looper,可是要注意只能初始化一次。ui
调用Looper.prepare()
方法初始化完成后,能够调用myLooper()
和myQueue()
方法获得当前线程对应的实例。this
public static @Nullable Looper myLooper() { return sThreadLocal.get(); } public static @NonNull MessageQueue myQueue() { return myLooper().mQueue; } 复制代码
退出Looperspa
退出Looper有安全与不安全两种退出方法,其实对应的就是MessageQueue的安全与不安全方法:
public void quit() { mQueue.quit(false); } public void quitSafely() { mQueue.quit(true); } 复制代码
什么安全退出,什么是不安全退出,在MessageQueue源码中分析过。
调用Looper.prepare()
方法初始化完成Looper后就可让Looper去工做了,只须要调用Looper.loop()
方法便可。
public static void loop() { // 获得当前线程下的Looper final Looper me = myLooper(); // 若是还没初始化过抛异常 if (me == null) { throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); } // 获得当前线程下与Looper对应的消息队列 final MessageQueue queue = me.mQueue; // 获得当前线程的惟一标识(uid+pid),做用是下面每次循环都判断一下线程有没有被切换 // 不知道为何要调用两次该方法 Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity(); // 进入死循环不断取出消息 for (;;) { // 从队列中取出一个消息,这可能会阻塞线程 Message msg = queue.next(); // 若是消息是空的,说明队列已经退出了,直接结束循环,结束方法 if (msg == null) { return; } // 打印日志 final Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } // 性能分析相关的东西 final long traceTag = me.mTraceTag; if (traceTag != 0 && Trace.isTagEnabled(traceTag)) { Trace.traceBegin(traceTag, msg.target.getTraceName(msg)); } try { //尝试将消息分发给宿主(Handler) //dispatchMessage为宿主Handler的接收消息方法 msg.target.dispatchMessage(msg); } finally { // 性能分析相关的东西 if (traceTag != 0) { Trace.traceEnd(traceTag); } } //打印日志 if (logging != null) { logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); } //获得当前线程的惟一标识 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(); } } 复制代码
Looper的功能很简单,核心方法Looper.loop()
就是不断的从消息队列中取出消息分发给对应的宿主Handler,它与对应MessageQueue息息相关,一块儿建立,一块儿退出。
Looper更想强调的是线程的独立性与惟一性,利用ThreadLocal
保证每一个线程只有一个Looper实例的存在。利用静态构造实例方法保证不能重复建立Looper。
Looper.prepareMainLooper()
是比较特殊的方法,它是给UI线程准备,理论上开发者在任何状况下都不该该调用它。