在实际开发中 Looper、Handler、Message 经常使用,或者在面试的时时候经常被提到,这里从源码解析深入理解Handler机制,知其然知其所以然。
Handler 是 Android SDK 提供给开发者方便进行异步消息处理的类。我们知道不能在主线程处理耗时操作,例如网络请求,读写操作...这时候就需要今天的主角Handler,子线程进行耗时操作,将请求结果通过Handler的sendMessge**() 方法发送出去,在主线程中通过Handler的handleMessage 方法处理请求结果,进行UI的更新。
(异步消息处理线程启动后会进入一个无限的循环体之中,每循环一次,从其内部的消息队列中取出一个消息,然后回调相应的消息处理函数,执行完成一个消息后则继续循环,若消息队列为空,线程则会阻塞。这里就需要 Handler、Looper、Message、MessageQueue,那他们之间又是什么联系了?)
public final class Message implements Parcelable {
public int what;
public int arg1;
public int arg2;
public Object obj;
Handler target;
Runnable callback;
/**
* Return a new Message instance from the global pool. Allows us to
* avoid allocating new objects in many cases.
*/
public static Message obtain() {
synchronized (sPoolSync) {
if (sPool != null) {
Message m = sPool;
sPool = m.next;
m.next = null;
m.flags = 0; // clear in-use flag
sPoolSize--;
return m;
}
}
return new Message();
}
/** Constructor (but the preferred way to get a Message is to call {@link #obtain() Message.obtain()}).
*/
public Message() {
}
}
里边有提取了四个我们常用熟悉的属性。:
1.Message 实现了Parcelable 接口,也就是说实现了序列化,可以Message在不同进程之间传递。
2. 包含一个名为target的Handler 对象
3. 包含一个名为callback的Runnable 对象
4. 使用obtain 方法可以从消息池中获取Message的实例,(推荐使用这个)
2.1、Looper.prepare()方法:
public static final void prepare () {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(true));
}
sThreadLocal.set(new Looper(true)),其中sThreadLocal是ThreadLocal对象,在一个线程中存储变量。在第2-4行中判断sThreadLocal是否为null,否则就会报错,也就是说一个线程中只能调用一次Looper.prepare()方法,保证了一个线程中保存一个Looper实例。再来看看new Looper(true)
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
可以看到第2行new MessageQueue(quitAllowed)创建一个MessageQueue消息队列,因为Looper.prepare()在一个线程中只能调用一次,所以MessageQueue在一个线程中也只会存在一个。
2.2、Looper.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;
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// This must be in a local variable, in case a UI event sets the logger
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 {
msg.target.dispatchMessage(msg);
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
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();
}
}
第2行myLooper()方法:
public static Looper myLooper() {
return sThreadLocal.get();
}
返回了sThreadLocal存储的Looper实例,如果me为null则抛出异常,所以这里Looper.prepare()方法要在Looper.loop()之前执行,否则就会报错。
final MessageQueue queue = me.mQueue;
拿到该looper实例中的mQueue(消息队列)
for (;;) {
...
}
这里才是核心代码进入无限循环,取出一条消息交给msg.target.dispatchMessage(msg)处理,(其中msg.target就是我们关联的handler,下面会说)。
前面说了很多大佬是该出现了!
用代码来解释可能更好理解,在这之前我们先创建一个handler对象
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.arg1 == 1) {
Toast.makeText(ThreadPoolActivity.this, "handler", Toast.LENGTH_SHORT).show();
}
super.handleMessage(msg);
}
};
再来看看handler构造函数:
public Handler() {
this(null, false);
}
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;
}
代码看起很多,不用慌我们找重点:
mLooper = Looper.myLooper();
这行代码获取了当前线程的Looper(前面有说过,一个线程只会保存一个Looper,如果为null,那就会报错,所以这里不用担心Looper对象是不是自己想要的!)
mQueue = mLooper.mQueue;
这行代码就是获取到了MessageQueue(消息队列),这样handler就和我们的MessageQueue(消息队列)关联了起来。
那么消息是从哪里来?我们熟悉的handler.sendMessage()来了:
handler.sendMessage()方法最后会调用sendMessageAtTime()方法,
public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
if (queue == null) {
RuntimeException e = new RuntimeException(
this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
return false;
}
return enqueueMessage(queue, msg, uptimeMillis);
}
看最后一个方法enqueueMessage();再看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; msg.target赋值为this(就是把当前的handler作为msg的target属性),最后调用queue的enqueueMessage的方法,将handler发出的消息添加到MessageQueue消息队列中。(在前面Looper.loop()方法中,取出每个msg然后交给msg.target.dispatchMessage(msg)去处理消息,也就是handler.dispatchMessage(msg))
handler.dispatchMessage(msg):
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
可以看到最后调用了 handleMessage(msg);我们很熟悉的方法,在这个方法回调里就可以根据msg.what进行消息处理更新UI。
1、Looper.prepare()在本线程中保存一个Looper实例,创建一个MessageQueue对象,与当前线程绑定。
2、Looper.loop()负责从MessageQueue消息队列中读取消息,回调msg.target.dispatchMessage(msg)方法。
3、Handler构造方法会与Looper、MessageQueue相关联。
4、Handler的sendMessage方法,通过 msg.target = this; msg.targe,会给msg的target赋值为handler自身,然后加入MessageQueue中。
5、在构造Handler实例时,我们会重写handleMessage方法,等于msg.target.dispatchMessage(msg)最终调用的方法。
6、Looper.loop() 消息循环是一个线程开启循环模式,持续监听并依次处理其他线程给它发的消息。