时序图:
Handler机制原理分析,基于android13的代码分析,其代码时序图如下:
主要的类说明:
Handler.java:
解释:
用于发送和处理消息Message的类,有两个作用:
(1)调度消息和在未来某个时间点执行的可运行文件;
(2)在主线程排队发送一个消息给子线程;
主要方法:
//发送消息
public final boolean sendEmptyMessage(int what);
public final boolean sendEmptyMessageDelayed(int what, long delayMillis)
public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis)
public boolean sendMessageAtTime(@NonNull Message msg, long uptimeMillis)
//接受消息:
public void handleMessage(@NonNull Message msg);
发送消息:Message
应用程序new Handler后,会调用sendMessageAtTime(msg)方法把Message消息体发送给MessageQueue队列,如下图:
public boolean sendMessageAtTime(@NonNull 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);
}
//发送消息给队列
private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg,
long uptimeMillis) {
msg.target = this;
msg.workSourceUid = ThreadLocalWorkSource.getUid();
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
然后再调用MessageQueue.enqueueMessage(msg, uptimeMillis)方法,发送msgMessageQueue队列;
MessageQueue.java:
解释:保存要由循环器发送的消息列表。消息不会直接添加到MessageQueue中,而是通过与Looper关联的Handler对象添加。可以使用Looper.myQueue()检索当前线程的MessageQueue。
主要方法:
boolean enqueueMessage(Message msg, long when):入队方法:存
Message next() :出队方法:取
enqueueMessage(msg, uptimeMillis)方法在接收到msg后,会把msg存入列表,代码如下:
boolean enqueueMessage(Message msg, long when) {
//***省略代码***//
msg.markInUse();
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;
}
//**** 省略代码***//
}
return true;
}
MessageQueue入、出消息队列原理图如下:
Looper.java:
解释:
用于为线程运行消息循环的类。默认情况下,线程没有与之关联的消息循环;要创建一个,在要运行循环的线程中调用prepare,然后循环让它处理消息,直到循环停止。与消息循环的大多数交互都是通过Handler类进行的。
出队列:
Looper.loop()是一直循环取message 的方法,具体代码如下 :
public static void loop() {
final Looper me = myLooper();//取出当前进程的Looper
//****省略代码***//
for (;;) {
if (!loopOnce(me, ident, thresholdOverride)) {
return;
}
}
}
private static boolean loopOnce(final Looper me,
final long ident, final int thresholdOverride) {
Message msg = me.mQueue.next(); // 取出队列中的下一个Message,可能会阻塞
if (msg == null) {
// No message indicates that the message queue is quitting.
return false;
}
///**** 省略代码****//
try {
msg.target.dispatchMessage(msg);//发送message给Handler
if (observer != null) {
observer.messageDispatched(token, msg);
}
dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
} catch (Exception exception) {
if (observer != null) {
observer.dispatchingThrewException(token, msg, exception);
}
throw exception;
} finally {
ThreadLocalWorkSource.restore(origWorkSource);
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
///**** 省略代码****//
return true;
}
在loopOnce()方法中去调用Message msg = me.mQueue.next()这一行代码来取出队列中的Message,mQueue.next() 方法中会有for循环一直取message,代码如下:
Message next() {
//****省略代码****//
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
nativePollOnce(ptr, nextPollTimeoutMillis);
synchronized (this) {
// Try to retrieve the next message. Return if found.
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = mMessages;
if (msg != null && msg.target == null) {
// Stalled by a barrier. Find the next asynchronous message in the queue.
do {
prevMsg = msg;
msg = msg.next;
} while (msg != null && !msg.isAsynchronous());
}
if (msg != null) {
if (now < msg.when) {
// Next message is not ready. Set a timeout to wake up when it is ready.
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
// Got a message.
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
if (DEBUG) Log.v(TAG, "Returning message: " + msg);
msg.markInUse();
return msg;//返回msg
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
//****省略代码****//
}
}
在next()方法中取到Message后调用Message.target.dispatchMessage(msg),把Message消息发送回主线程,代码如下,在主线程只要重新实现handleMessage方法,就可以接收到Message.
/**
* Handle system messages here.
*/
public void dispatchMessage(@NonNull Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
/**
* Subclasses must implement this to receive messages.
*/
public void handleMessage(@NonNull Message msg) {
}
Looper循环机制原理图:
Message.java:
解释:
定义一条包含描述和任意数据对象的消息,该消息可以发送给处理程序。此对象包含两个额外的int字段和一个额外的对象字段,在许多情况下允许您不进行分配。
虽然Message的构造函数是公共的,但获取其中之一的最佳方法是调用Message.obtain() 或者 Handler.obtainMessage()方法之一,这将从回收对象池中提取它们。
Looper创建和启动时机:
当Handler发送Message的,那么Looper的循环取Message是如何启动的:
当一个应用进程在创建时,其ActivityThread会首先执行ActivityThread.main()方法,然后会分别调用Looper.prepareMainLooper()和Looper.loop(),如下:
//ActivityThread.java
public static void main(String[] args) {
//****省略代码****//
Process.setArgV0("<pre-initialized>");
Looper.prepareMainLooper();//创建Looper
//****省略代码****//
Looper.loop();//开始循环
//****省略代码****//
}
//Looper.java
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
@Deprecated
public static void prepareMainLooper() {
prepare(false);
synchronized (Looper.class) {
if (sMainLooper != null) {
throw new IllegalStateException("The main Looper has already been prepared.");
}
sMainLooper = myLooper();
}
}
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对像后存入到ThreadLocal<Looper>
}
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
MessageQueue创建时机:
MessageQueue是在应用进程启动时,ActivityThread.main()执行时,调用prepareMainLooper()-->prepare(boolean quitAllowed)时创建,如下代码:
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));
}
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);//创建MessageQueue
mThread = Thread.currentThread();
}