开发中我们经常使用 Handler 来进行线程间的通信。这篇我们就从源码角度来分析一下 Handler 是怎么工作的。
使用
Handler
先来看一下我们最常用的使用方式,这种创建方式,会使用当前线程的 Looper 创建一个 Handler。关于 Looper 下面在分析。
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
return false;
}
});
handler.post(new Runnable() {
@Override
public void run() {
//do something
}
});
HandlerThread
系统还为我们提供了另外一种使用方式,就是 HandlerThread 。
HandlerThread handlerThread = new HandlerThread("HandlerThread");
Handler handler = new Handler(handlerThread.getLooper(),new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
return false;
}
});
handler.post(new Runnable() {
@Override
public void run() {
//do something
}
});
查看 HandlerThread 源码可以看到其继承自 Thread。在 run()
方法中分别调用了 Looper 的 prepare()
、loop()
两个方法。
HandlerThread handlerThread = new HandlerThread("HandlerThread");
Handler handler = new Handler(handlerThread.getLooper(),new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
return false;
}
});
handler.post(new Runnable() {
@Override
public void run() {
//do something
}
});
public class HandlerThread extends Thread {
@Override
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
}
猜想
如果我们在子线程种直接创建 Handler 的话,不调用 Looper 的相关方法会发生什么事?
经过试验发现会抛出如下 RuntimeException 。意思就是,当前线程没有调用 Looper.prepare()
方法。
public Handler(@Nullable Callback callback, boolean async) {
......
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread " + Thread.currentThread()
+ " that has not called Looper.prepare()");
}
......
}
那我们就进入 Looper.prepare() 方法,看一下这里面都干了啥事,不掉这玩意还就不工作了。可以看到,这个类注释提供了一个在子线程中使用 Handler 的例子,和 HandlerThread 创建方式是一致的。而 prepare 方法就是创建一个 Looper 对象,设置给当前线程。
/**
* 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();
* }
* }
*/
public final class Looper {
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 这个玩意,这东西到底是干啥用的呢?下面就从源码入手分析 Looper 到底干了啥。
源码分析
Looper
从 Looper 的源码中不难发现,当调用 loop()
方法后,会执行一个死循环,当前线程会一直在 for 循环中取出消息和分发消息,此时,当前线程就具备消息轮训的功能了。
/**
* 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.
*
* 大致意思就是,作为线程运行的消息循环类。提供消息的添加和移除操作。
*/
public final class Looper {
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
public static void loop() {
for (;;) {
//标签①
//might block , 此处有可能阻塞
Message msg = queue.next();
......
msg.target.dispatchMessage(msg);
......
}
}
}
public final class MessageQueue {
MessageQueue(boolean quitAllowed) {
mQuitAllowed = quitAllowed;
mPtr = nativeInit();
}
private native static long nativeInit();
}
创建 Looper 对象的同时创建了 MessageQueue ,这个 MessageQueue 只有消息队列的功能,提供了消息的添加和获取功能。
public final class MessageQueue {
MessageQueue(boolean quitAllowed) {
mQuitAllowed = quitAllowed;
mPtr = nativeInit();
}
private native static long nativeInit();
}
上面 标签① 中说明了,MessageQueue 的 next 方法有可能会发生阻塞,但是这个类并没有实现相应的阻塞和唤起功能,我们在进入到 nativeInit 方法查看。
该类对应的 JNI 类是 android_os_MessageQueue.cpp,在构造函数中又创建了一个 native 的 Looper 对象。
static jlong android_os_MessageQueue_nativeInit(JNIEnv* env, jclass clazz) {
NativeMessageQueue* nativeMessageQueue = new NativeMessageQueue();
if (!nativeMessageQueue) {
jniThrowRuntimeException(env, "Unable to allocate native queue");
return 0;
}
nativeMessageQueue->incStrong(env);
return reinterpret_cast<jlong>(nativeMessageQueue);
}
NativeMessageQueue::NativeMessageQueue() :
mPollEnv(NULL), mPollObj(NULL), mExceptionObj(NULL) {
mLooper = Looper::getForThread();
if (mLooper == NULL) {
mLooper = new Looper(false);
Looper::setForThread(mLooper);
}
}
在进入 Looper.cpp 的构造函数,发现在其构造函数中调用了 eventfd 函数,创建了一个事件对象,老版本是使用管道(pipe)实现的。
eventfd函数会创建一个eventfd,这是一个计数器相关的fd,计数器不为零是有可读事件发生,read以后计数器清零,write递增计数器;返回的fd可以进行如下操作:read、write、select(poll、epoll)、close。
Looper::Looper(bool allowNonCallbacks) :
mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
mPolling(false), mEpollFd(-1), mEpollRebuildRequired(false),
mNextRequestSeq(0), mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
mWakeEventFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
LOG_ALWAYS_FATAL_IF(mWakeEventFd < 0, "Could not make wake event fd: %s",
strerror(errno));
AutoMutex _l(mLock);
rebuildEpollLocked();
}
void Looper::rebuildEpollLocked() {
......
// Allocate the new epoll instance and register the wake pipe.
mEpollFd = epoll_create(EPOLL_SIZE_HINT);
......
int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeEventFd, & eventItem);
......
}
关于 epoll 机制大概知道如下几个方法的功能,就不影响源码的阅读,如果想深入了解 epoll 机制,可以查看 Linux 的 epoll 机制
函数 | 功能 |
---|---|
epoll_create | 创建一个 epoll 句柄 |
epoll_ctl | 将被监听的描述符添加到epoll句柄或从epool句柄中删除或者对监听事件进行修改 |
epoll_wait | 等待事件触发,当超过timeout还没有事件触发时,就超时。 |
读取数据
通过上面分析,当先线程最终都会调用 Looper 的 loop()
方法进入消息循环,我们就看下这个方法干了啥。可以看到注释说明,这里有可能阻塞。
可以看到在 next 方法中 调用了 native 的 nativePollOnce 方法后,获取到了一个 message 进行返回。然后将返回后的 message 进行 dispatch 派发。就会在我们注册的回调函数中收到发送的消息,然后进行相应的处理。
public final class Looper {
public static void loop() {
......
final MessageQueue queue = me.mQueue;
......
for (;;) {
Message msg = queue.next(); // might block
......
msg.target.dispatchMessage(msg);
......
}
}
}
public final class MessageQueue {
Message next() {
......
for (;;) {
......
//标注①
nativePollOnce(ptr, nextPollTimeoutMillis);
synchronized (this) {
......
Message prevMsg = null;
Message msg = mMessages;
......
// Got a message.
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
msg.markInUse();
return msg;
......
}
}
}
private native void nativePollOnce(long ptr, int timeoutMillis);
}
我们接着继续深入 标签① 中的 native 方法,看看这里干了啥事。顺着调用链往下走,发现最终会走到 native 中的 Looper 的 pollInner 方法。最终会在 标签② 的 epoll_wait 方法处发生阻塞,等待 epoll 机制的唤醒,然后返回。
这里返回后,我们上面 标签① 处的方法就可以继续往下执行了,最后进行消息的分发。
static void android_os_MessageQueue_nativePollOnce(JNIEnv* env, jobject obj,
jlong ptr, jint timeoutMillis) {
NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr);
nativeMessageQueue->pollOnce(env, obj, timeoutMillis);
}
void NativeMessageQueue::pollOnce(JNIEnv* env, jobject pollObj, int timeoutMillis) {
......
mLooper->pollOnce(timeoutMillis);
......
}
int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
......
result = pollInner(timeoutMillis);
......
}
int Looper::pollInner(int timeoutMillis) {
......
struct epoll_event eventItems[EPOLL_MAX_EVENTS];
//标签②
int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
......
for (int i = 0; i < eventCount; i++) {
int fd = eventItems[i].data.fd;
uint32_t epollEvents = eventItems[i].events;
if (fd == mWakeEventFd) {
if (epollEvents & EPOLLIN) {
//标签③
awoken();
} else {
ALOGW("Ignoring unexpected epoll events 0x%x on wake event fd.", epollEvents);
}
}
......
}
}
发送数据
OK,接下来我们在看看数据的发送。这里的调用比较简单,顺着 post()
方法往下走,最终会调用了 queue.enqueueMessage()
方法。注释都已经加上,可以大致看下。
public class Handler {
public final boolean post(@NonNull Runnable r) {
return sendMessageDelayed(getPostMessage(r), 0);
}
/**
* 将当前 runnable 封装到一个 msg 中返回
*/
private static Message getPostMessage(Runnable r) {
Message m = Message.obtain();
m.callback = r;
return m;
}
public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis) {
// delayMillis 合法校验
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
public boolean sendMessageAtTime(@NonNull Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
// queue 检测,如果为空,则当前消息循环队列不可用,返回 false
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);
}
private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg,
long uptimeMillis) {
// 将当前的 Hanlder 赋值到 msg 的 target 中。
// 读取到消息后,通过 msg.target.dispatchMessage(msg) 分发消息
msg.target = this;
msg.workSourceUid = ThreadLocalWorkSource.getUid();
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
}
接着进入 MessageQueue 的 enqueueMessage 方法。具体的注释已经添加到代码上。
public final class MessageQueue {
boolean enqueueMessage(Message msg, long when) {
......
//设置发生的时间
msg.when = when;
Message p = mMessages;
//是否需要唤起
boolean needWake;
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
// 当前队列是空的,将消息插入消息头
msg.next = p;
mMessages = msg;
needWake = mBlocked;
} else {
// Inserted within the middle of the queue. Usually we don't have to wake
// up the event queue unless there is a barrier at the head of the queue
// and the message is the earliest asynchronous message in the queue.
needWake = mBlocked && p.target == null && msg.isAsynchronous();
Message prev;
for (;;) {
prev = p;
p = p.next;
// 将消息按照时间排序插入相应的位置
if (p == null || when < p.when) {
break;
}
if (needWake && p.isAsynchronous()) {
needWake = false;
}
}
msg.next = p; // invariant: p == prev.next
prev.next = msg;
}
// We can assume mPtr != 0 because mQuitting is false.
// 如果当前队列处于 wait 状态。则唤起 native wait
if (needWake) {
nativeWake(mPtr);
}
......
}
private native static void nativeWake(long ptr);
}
这个 native 同样是在 android_os_MessageQueue.cpp。这里最终在 Looper 中调用 write 方法后,事件通知有事件写入。最终会唤起 标签② 处的 wait 方法,继续执行 标签② 之后的代码,获取消息,执行分发。
static void android_os_MessageQueue_nativeWake(JNIEnv* env, jclass clazz, jlong ptr) {
NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr);
nativeMessageQueue->wake();
}
void NativeMessageQueue::wake() {
mLooper->wake();
}
void Looper::wake() {
......
uint64_t inc = 1;
ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd, &inc, sizeof(uint64_t)));
......
}
总结
到这里 Handler 中消息的循环机制、分发都已经说完了,这里在附上一个流程图。
Q & A
-
为什么主线程用 Looper 死循环不会引发 ANR?
因为在 Looper.next() 开启死循环时,一旦需要等待或者还未到执行的时候,会调用 NDK 中的 JNI 方法,释放当前的时间片,这样就不会 ANR 了。 -
为什么 Handler 的构造方法中不直接 new 一个 Looper 呢?
在构造方法中直接 new 无法保证唯一性。只有通过 Looper.prepare() 方法能保证其唯一性。
最开始说的经常使用的方式,是因为在主线程中直接创建的,而主线程在创建的时候就已经调用 Looper 的 prepare 和 loop 方法,将当前线程作为一个消息轮训线程了,直接使用了主线程的消息循环模型。
从而揭示了 Android 本身就是基于消息循环模型而执行相关操作的。