handler工作原理分析 MessageQueue Looper Handler

要了解Handler的工作原理,首先要了解ThreadLocal。

ThreadLocal 是一个线程内部的数据存储类,通过它可以在指定线程内存储数据,并且其他线程无法获取到这些数据。它的一个典型应用是 Looper,它的另一个使用场景是复杂逻辑下的对象传递,例如监听器的传递。当一个线程内的任务过于复杂,而又需要监听器贯穿整个线程的执行过程时,可以使用 ThreadLocal,将监听器作为线程的全局对象存在,线程内部通过 get 即可获取到监听器。若不采用 ThreadLocal,则必须将监听器通过参数在函数调用栈中传递,当调用栈很深的时候,这种方法是不可接受的;也可将监听器作为静态变量供线程访问,这种方法不具备扩充性,比如有两个线程在执行则必须提供两个静态对象。当线程过多时也是存在问题的。而使用 ThreadLocal 则可以将每个监听器对象在自己的线程中存储。

可通过以下代码来创建一个 ThreadLocal:

1
private ThreadLocal<Boolean> mThreadLocal = new ThreadLocal<Boolean>();

通过调用它的 set 和 get 方法来实现对数据的存储:

1
2
mThreadLocal.set(true);
mThreadLocal.get();

下面我们来看一下它的 set 和 get 方法:

1
2
3
4
5
6
7
8
public void (T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}

在 set 方法中,根据当前所处的是哪个线程,通过 getMap 从 ThreadLocalmap 中取得当前线程的 ThreadLocalMap 对象,并调用其 set 方法,在方法内部有一个 tab 数组专门用来存储数据。若为空,则创建并将数据存储进去。ThreadLocalMap 是 ThreadLocal 的静态内部类,用于存储线程及线程对应的数据。

get方法如下:

1
2
3
4
5
6
7
8
9
10
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}

get 方法与 set 方法类似,也是获取当前线程的 ThreadLocalMap 对象,并根据当前的 ThreadLocal 对象通过 map.getEntry(this) 获取到对应的值。

MessageQueue

通过 enqueueMessage 和 next 分别往消息队列中插入一条消息与取出一条消息并将其从队列中移除,但其内部实现并不是用的队列,而是采用单链表的数据结构。enqueueMessage 主要是单链表的插入操作,而 next 方法是一个无限循环的方法,若队列中没有消息,那么 next 方法会一直阻塞在那里,当新消息到来时,next 方法会返回这条消息并将其从单链表中移除。

Looper

通过 Looper.prepare() 来创建一个 Looper

1
2
3
4
5
6
7
8
9
10
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));
}

Looper 的构造器:

1
2
3
4
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}

Looper 的构造方法中会创建一个消息队列。

Handler 工作需要 Looper,平时在主线程没创建 Looper 是因为系统帮我们创建好了,可通过 getMainLooper 来获取主线程的 Looper。通过 Looper.prepare() 为当前线程创建一个 Looper,接着通过 Looper.loop() 来开启消息循环。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;
// 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 slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
final long traceTag = me.mTraceTag;
if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
}
final long start = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
final long end;
try {
msg.target.dispatchMessage(msg);
end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
......
}

loop 方法会不停地从 MessageQueue 中查看是否有新消息,如果有就会调用 msg.target.dispatchMessage(msg) 来处理,否则就一直阻塞在那里,除非 next 方法返回 null。msg.target 就是发送这条消息的 Handler 对象。可通过 quit 和 quitSafely 使 next 方法返回 null 来退出一个 Looper,后者会等消息队列中所有消息处理完毕后才安全退出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void quit(boolean safe) {
if (!mQuitAllowed) {
throw new IllegalStateException("Main thread not allowed to quit.");
}
synchronized (this) {
if (mQuitting) {
return;
}
//标志位,使 next 方法返回 null
mQuitting = true;
if (safe) {
removeAllFutureMessagesLocked();
} else {
removeAllMessagesLocked();
}
// We can assume mPtr != 0 because mQuitting was previously false.
nativeWake(mPtr);
}
}

Handler

handler 主要工作包括消息的发送和接收。消息的发送可通过 post 和 send 的一系列方法来实现,post 最终是通过 send 的一系列方法来实现的。发送消息的过程仅仅是向 MessageQueue 中插入一条消息,MessageQueue 的 next 方法会返回这条消息给 looper,Looper 收到消息后就开始处理,最终由 looper 交给 handler 的dispatchMessage 处理。
dispatchMessage 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
//msg 的 callback 其实就是 post 方法传递的 Runnable 参数
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}

主要说一下 mCallback,可通过 Callback 创建 Handler 对象:
Handler handler = new Handler(callback);
这样就可以不用派生 Handler 的子类并重写其 handleMessage 方法。
以上过程可用以下一张图表示:
流程图

总结

Handler 的内部主要执行过程:

  1. 通过 Looper.prepare() 创建 Looper 并与当前线程的 ThreadLocal 对象绑定,Looper 对象会创建一个 MessageQueue。
  2. 创建 Handler 对象,对象内部会通过 Looper 得到当前线程内的 MessageQueue。
  3. 调用 Handler 的 post 或 send 方法发送消息,会将消息入队。
  4. 调用 Looper.loop() 方法,不断从消息队列中取得消息,若无消息,则阻塞;若有消息,则回调回 Handler 的方法。这里的方法主要有三种:
    • 在创建 Handler 对象时重写其 handlerMessage 方法
    • 在创建 Handler 对象时采用了 Handler handler = new Handler(callback); 构造方法,最终通过 callback 回调。
    • Handler 采用 post(Runnable r) 来发送消息,最终回调 r.run() 方法。

参考自《Android开发艺术探索》第十章