Looper的作用有两点:
Looper的静态方法prepare()中,会给线程局部存储添加一个新的Looper对象,而Looper的构造函数中,会创建一个MessageQueue消息队列,代码如下:
/** Initialize the current thread as a looper. * This gives you a chance to create handlers that then reference * this looper, before actually starting the loop. Be sure to call * {@link #loop()} after calling this method, and end it by calling * {@link #quit()}. */ public static final void prepare() { if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper()); }
private Looper() { mQueue = new MessageQueue(); mRun = true; mThread = Thread.currentThread(); }对程序员来讲,当需要把一个线程变为异步消息处理线程的时候,应该在Thread类的run()函数中先调用Looper.prapare()为该线程创建一个MessageQueue对象,然后再调用Looper.loop()函数,使当前线程进入消息处理循环。使用范例,代码如下:
/** * Class used to run a message loop for a thread. Threads by default do * not have a message loop associated with them; to create one, call * {@link #prepare} in the thread that is to run the loop, and then * {@link #loop} to have it process messages until the loop is stopped. * * <p>Most interaction with a message loop is through the * {@link Handler} class. * * <p>This is a typical example of the implementation of a Looper thread, * using the separation of {@link #prepare} and {@link #loop} to create an * initial Handler to communicate with the Looper. * * <pre> * 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(); * } * }</pre> */
loop()函数的代码如下:
/** * Run the message queue in this thread. Be sure to call * {@link #quit()} to end the loop. */ public static final void loop() { Looper me = myLooper(); MessageQueue queue = me.mQueue; while (true) { Message msg = queue.next(); // might block //if (!me.mRun) { // break; //} if (msg != null) { if (msg.target == null) { // No target is a magic identifier for the quit message. return; } if (me.mLogging!= null) me.mLogging.println( ">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what ); msg.target.dispatchMessage(msg); if (me.mLogging!= null) me.mLogging.println( "<<<<< Finished to " + msg.target + " " + msg.callback); msg.recycle(); } } }该段代码的执行流程如下:
1. 调用myLooper()函数返回当前线程的Looper对象,该函数内部仅仅通过调用sThreadLocal.get()方法返回当前线程id对应的Looper对象。
/** * Return the Looper object associated with the current thread. Returns * null if the calling thread is not associated with a Looper. */ public static final Looper myLooper() { return (Looper)sThreadLocal.get(); }
2. 进入while(true)无限循环
1) 调用MessageQueue对象的next()函数取出队列中的消息。注意,如果当前队列为空,则当前线程会被挂起,也就是说,next()函数内部会暂停当前线程。
2) 回调msg.target.dispatchMessage()函数,完成对消息的处理,也就是说,消息的具体处理实际上是由程序指定的。msg变量的类型是Message,msg.target的类型是Handler。
3) 每处理完该消息后,需要调用msg.recycle()回收该Message对象占用的系统资源。因为Message内部使用了一个数据池保存Message对象,从而避免不停地创建和删除Message对象。因此,每次处理完该消息后,需要将该Message对象设置为空闲状态,以便使该Message对象可以被重用。