Android消息机制详解
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Android消息机制详解
Q1:什么是Android消息机制?
我们知道Android是基于消息循环机制的,即在主线程上开启⼀个⽆限循环,在这个循环中有⼀个Looper,它的职责就是不断地从消息队列(MessageQueue)中取出消息,交给消息关联的处理器(Handler)去处理。
Q2:为什么需要了解Android消息机制,它和我们⾃⼰写的Android代码有何关联?
可以说Android消息机制就是Android应⽤程序的核⼼,我们所编写的应⽤程序代码也是运⾏在消息机制之上,为什么这么说?因为Android 应⽤程序的编写主要是围绕Android四⼤组件展开的,更准确的说都是我们所编写的代码都位于组件的⽣命周期函数中,⽽组件的⽣命周期⼜是在ActivityThread的内部类H的handleMessage⽅法中被调⽤的。
可能会有⼈说,我的代码是写在⼀个单独的类中,并不是处于组件的⽣命周期⽅法中。
其实写在哪⾥并不重要,重要的是它在哪⾥被调⽤的,在整个调⽤栈中是否包含组件⽣命周期⽅法,除⾮你写的这个类并没有被⽤到,那⾃然不存在调⽤栈。
Q3:Android消息机制是如何实现的?
这个问题需要从源码中寻求真相,ok,read the fucking source code
先从熟悉的⼊⼿,handler是我们在平时开发中接触的⽐较多的,OK,就它了。
Handler
类的注释
A Handler allows you to send and process {@link Message} and Runnable
objects associated with a thread's {@link MessageQueue}. Each Handler
instance is associated with a single thread and that thread's message
queue. When you create a new Handler, it is bound to the thread /
message queue of the thread that is creating it -- from that point on,
it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
⾕歌翻译:Handler允许您发送和处理与线程{@link MessageQueue}关联的{@link Message}和Runnable 对象。
每个Handler 实例都与⼀个线程和该线程的消息队列相关联。
当你创建⼀个新的Handler时,它被绑定到正在创建它的线程的线程/ 消息队列 - 从那时起,它将消息和runnables传递给该消息队列并在它们出现时执⾏它们。
There are two main uses for a Handler: (1) to schedule messages and
runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.
这⾥还附带说明了Handler的两个主要⽤途:
1. ⽤于调度messages和runnables在将来某个时间点执⾏,也就是做延时操作。
2. 可以⼊队⼀个不在当前线程执⾏的任务,即做线程切换。
虽然之前没注意看handler类的注释,但好像还都⽤对了,平时好像也就是⽤它来做以上两种操作。
构造⽅法
public Handler(Looper looper, Callback callback, boolean async) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
looper:前⾯提到过,消息分发器,不断从消息队列中拿消息,并分发给Handler处理
callback:定义了handleMessage⽅法,不过它和handler内部⽅法handleMessage有所不同,它是有boolean类型的返回值,它的优先级⽐内部的handleMessage⾼,返回true代表该消息它已经处理完毕,不需要向下分发给内部的handleMessage⽅法,false则代表可以向下分发。
public interface Callback {
/**
* @param msg A {@link android.os.Message Message} object
* @return True if no further handling is desired
*/
public boolean handleMessage(Message msg);
}
async:如果该值为 true,则由此Handler发送的消息都被设置msg.setAsynchronous(true),则代表该消息有较⾼的优先级,Looper在拿到消息会判断是否是屏障消息(关于什么是屏障消息后⾯会提到)时都会遍历消息列表,查找是否有异步消息,如果有则优先执⾏,系统在发出界⾯绘制消息时就会设置这个标记,以便该消息能够优先被处理,为了保证界⾯绘制任务的优先级,所以我们初始化handler时保留它为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;
}
我们在对Handler进⾏初始化时,基本上是对以上构造函数的重载,即不传递Looper对象,此时它会调⽤Looper.myLooper()获取当前线程
的Looper对象,如果为空,则会抛出运⾏时异常,程序会挂掉。
这也解释了⼀个问题,为什么不能在⼀般的⼦线程创建Handler?这⾥说⼀般不可以,但是只要调⽤了Looper.prepare创建了⼦线程的Looper,⾃然就可以创建Handler了,⽐如系统的HandlerThread,后⾯也会分析到。
核⼼⽅法
public final boolean post(Runnable r)
{
return sendMessageDelayed(getPostMessage(r), 0);
}
添加⼀个Runnable到消息队列,该Runnable被处理的线程即时创建Handler的线程
public final boolean postDelayed(Runnable r, long delayMillis)
{
return sendMessageDelayed(getPostMessage(r), delayMillis);
}
添加⼀个Runnable到消息队列,并添加⼀定的延时时间
public final boolean sendMessage(Message msg)
{
return sendMessageDelayed(msg, 0);
}
发送⼀个消息到消息队列
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);
}
上⾯提到的3个⽅法添加的消息或任务最终都会被包装成Message对象被添加到消息对列中,接下来看看添加过程,即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进⾏了赋值,即将消息和Handler关联起来了,这⾥⽐较重要,因为分析Handler的内存泄漏和消息分发处理都与此有关。
然后就是设置消息异步,之前提到过。
接着调⽤了queue.enqueueMessage。
boolean enqueueMessage(Message msg, long when) {
//参数校验
if (msg.target == null) {
throw new IllegalArgumentException("Message must have a target.");
}
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}
synchronized (this) {
if (mQuitting) {
IllegalStateException e = new IllegalStateException(
msg.target + " sending message to a Handler on a dead thread");
Log.w(TAG, e.getMessage(), e);
msg.recycle();
return false;
}
//标记使⽤中
msg.markInUse();
//赋值when
msg.when = when;
//拿到链表头节点
Message p = mMessages;
//是否需要唤醒事件队列,其实也就是唤醒queue.next()
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.
//根据情况唤醒事件队列
if (needWake) {
nativeWake(mPtr);
}
}
return true;
}
我们可以知道MessageQueue其实是基于链表实现的,⽽且内部依据消息时间进⾏了排序。
到此,已经将Handler的⼀部分职责梳理清楚了,就是往消息队列中添加新消息。
下⾯进⼊消息机制第⼆个重要成员。
Looper
官⽅说明
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.
⾕歌翻译:⽤于为线程运⾏消息循环的类。
默认情况下,线程没有与它们关联的消息循环;创建⼀个,在运⾏循环的线程中调⽤ {@link #prepare},然后 {@link #loop}让它处理消息,直到循环停⽌。
官⽅还是⽜逼,⼀两句话就把Looper的职责和基本⽤法说清楚了,666
构造⽅法
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
很简单,创建了消息队列,quitAllowed参数表明这个消息队列是否允许退出(HandlerThread内部创建的是允许退出的,⽽主线程创建的是不允许退出的,因为⼀旦退出,应⽤就崩溃了),并记录下当前的线程。
但需要注意的是,构造函数是⽤private修饰的,意味着我们没法在外部通过new Looper()来创建Looper实例。
这种情况下⼀般都会提供静态⽅法来创建实例或者得到⼀个单例,当然Looper也是符合我们的⼀般情况的。
核⼼⽅法
prepare()
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实例,并保存在ThreadLocal中,⽽ThreadLocal是⼀个线程私有的成员变量,即只有在创建ThreadLocal线程中才能获取到正确的值。
prepareMainLooper
/**
* Initialize the current thread as a looper, marking it as an
* application's main looper. The main looper for your application
* is created by the Android environment, so you should never need
* to call this function yourself. See also: {@link #prepare()}
*/
public static void prepareMainLooper() {
prepare(false);
synchronized (Looper.class) {
if (sMainLooper != null) {
throw new IllegalStateException("The main Looper has already been prepared.");
}
sMainLooper = myLooper();
}
}
初始化主线程的Looper,这⾥prepare⽅法传⼊的是false,即消息队列是不允许退出的。
getMainLooper()
/**
* Returns the application's main looper, which lives in the main thread of the application.
*/
public static Looper getMainLooper() {
synchronized (Looper.class) {
return sMainLooper;
}
}
获取主线程的Looper,这个函数可以放⼼调⽤,因为prepareMainLooper是在ActivityThread的main函数中调⽤的,所以在应⽤⽣命周期的任何时间都能获得到正确的Looper。
myLooper()
/**
* Return the Looper object associated with the current thread. Returns
* null if the calling thread is not associated with a Looper.
*/
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
获得当前线程的Looper对象,这个函数可能返回空,因为当前线程可能并未调⽤Looper.prepare进⾏初始化。
looper()
/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
public static void loop() {
//验证Looper是否被初始化了
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
//next返回null,代表消息队列已经退出,可以结束循环
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
...省去⽇志打印信息
try {
//调⽤target⽅法对消息进⾏处理
msg.target.dispatchMessage(msg);
end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
...
}
}
1. 验证Looper是否初始化
2. 开启消息循环
3. 调⽤queue.next()拿消息,next可能是阻塞的,因为当消息队列中没有消息的时候,next阻塞住,直到我们或系统往队列中添加了消息,
然后next⽅法返回最新的消息。
4. 调⽤target即Handler的dispatchMessage对消息进⾏处理。
接下来看⼀下Message.next()具体实现
Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
final long ptr = mPtr;
//当queue退出的时候,ptr被置为0,这时返回null,此时looper也会退出循环
if (ptr == 0) {
return null;
}
int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
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;
//判断是不是屏障消息,如果则遍历链表找到异步消息,优先执⾏
//1.屏障消息的target为空,并且是直接插⼊到消息队列头部,⽬的是为了让绘制任务尽快被执⾏
//2.当系统有屏幕绘制请求时,会发送⼀个屏障消息到消息队列
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;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
// Process the quit message now that all pending messages have been handled.
if (mQuitting) {
dispose();
return null;
}
// If first time idle, then get the number of idlers to run.
// Idle handles only run if the queue is empty or if the first message
// in the queue (possibly a barrier) is due to be handled in the future.
//初始化闲时任务mPendingIdleHandlers,即当消息队列没有需要处理的消息时,
if (pendingIdleHandlerCount < 0
&& (mMessages == null || now < mMessages.when)) {
pendingIdleHandlerCount = mIdleHandlers.size();
}
if (pendingIdleHandlerCount <= 0) {
// No idle handlers to run. Loop and wait some more.
mBlocked = true;
continue;
}
if (mPendingIdleHandlers == null) {
mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
}
mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
}
// Run the idle handlers.
// We only ever reach this code block during the first iteration.
// 处理闲时任务
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler
boolean keep = false;
try {
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf(TAG, "IdleHandler threw exception", t);
}
//如果queueIdle返回true,代表保留该任务,false则执⾏完了就移除任务
if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}
// Reset the idle handler count to 0 so we do not run them again.
pendingIdleHandlerCount = 0;
// While calling an idle handler, a new message could have been delivered
// so go back and look again for a pending message without waiting.
nextPollTimeoutMillis = 0;
}
}
1. 拿到链头消息,判断是不是屏障消息,是就便利链表拿到异步消息返回,否则返回链头消息。
2. 当队列没有消息需要处理时,如果我们通过Looper.myQueue().addIdleHandler()添加了闲时任务时,就会开始处理我们的闲时任务。
这个闲时任务有些鸡肋,当主线程繁忙的时候,它可能⼀直得不到执⾏,⽽当主线程闲的时候,如果我们没有将它移除的话,它可能很快地被执⾏多次。
由于它执⾏时机的不确定性,暂时想不到很好的应⽤场景,以⾄于不看源码⼏乎不知道还有这么个机制。
接下来看Handler的dispatchMessage⽅法
/**
* Handle system messages here.
*/
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
private static void handleCallback(Message message) {
message.callback.run();
}
如果是任务消息,直接调⽤runnable.run直接执⾏,如果是普通消息,则先判断有⽆mCallback,即构造⽅法传⼊的Callback,有的则调
⽤mCallback.handleMessage,再根据返回值决定是否调⽤⾃⾝的handleMessage⽅法,也是我们继承Handler时需要重写的⽅法。
ok,到此Handler的职责分析完毕,就两个
添加消息到消息队列
处理Looper分发的消息
在⽣产者消费者模型中,Handler即是⽣产者,也是消费者。
MessageQueue
官⽅说明
Low-level class holding the list of messages to be dispatched by a
{@link Looper}. Messages are not added directly to a MessageQueue,
but rather through {@link Handler} objects associated with the Looper.
<p>You can retrieve the MessageQueue for the current thread with
{@link Looper#myQueue() Looper.myQueue()}.
⾕歌翻译:保存由{@link Looper}分派的消息列表的低级类。
消息不会直接添加到MessageQueue,⽽是通过与Looper关联的{@link Handler}对象添加。
您可以使⽤{@link Looper#myQueue()Looper.myQueue()}检索当前线程的MessageQueue。
我们可以得到3点信息
MessageQueue保存Looper的消息列表
通过Handler添加消息到MessageQueue
通过Looper.myQueue()获取当前线程的MessageQueue。
构造⽅法
MessageQueue(boolean quitAllowed) {
mQuitAllowed = quitAllowed;
mPtr = nativeInit();
}
quitAllowed:表⽰该消息队列是否允许退出,主线程默认是不允许的
nativeInit是⼀个本地⽅法,返回⼀个指针对象,并由mPtr保存,具体实现暂不深究
这⾥我的理解是:如果单纯地维护⼀个消息列表,根本不需要涉及c层代码,java层⾯完全搞定,但是要做到⾃由地低消耗地阻塞和唤醒,则需要借助Linux层⾯的⼀些技术.所以这⾥结合了两者,java层负责消息存储,c层负责进⾏阻塞和唤醒.
核⼼⽅法
enqueueMessage和next⽅法前⾯已经详介绍过了
postSyncBarrier
public int postSyncBarrier() {
return postSyncBarrier(SystemClock.uptimeMillis());
}
private int postSyncBarrier(long when) {
// Enqueue a new sync barrier token.
// We don't need to wake the queue because the purpose of a barrier is to stall it.
synchronized (this) {
final int token = mNextBarrierToken++;
//从消息池获得⼀个消息
final Message msg = Message.obtain();
msg.markInUse();
msg.when = when;
msg.arg1 = token;
Message prev = null;
Message p = mMessages;
if (when != 0) {
while (p != null && p.when <= when) {
prev = p;
p = p.next;
}
}
if (prev != null) { // invariant: p == prev.next
//插⼊链头
msg.next = p;
prev.next = msg;
} else {
//插⼊链表中间
msg.next = p;
mMessages = msg;
}
return token;
}
}
前⾯反复提到⼀个概念,屏障消息,这个⽅法就是说明屏障消息是如何被插⼊到消息队列的.
屏障消息最重要的⼀个特点就是没有相关联的Handler对象,即target属性为空,屏障消息的作⽤就不说了,反复提到过了.
还有⼀个点就是,消息队列中所引⽤的时间都是SystemClock.uptimeMillis(),即从开机到当前时间点的时间长度,为什么不是我们熟悉
的System.currentMillions(),因为这个时间并不准确,它可以受到⼈为⼲预,⽽对于⼀个以时间作为排序依据的消息队列来说,这个肯定是不能接受的. removeSyncBarrier
public void removeSyncBarrier(int token) {
// Remove a sync barrier token from the queue.
// If the queue is no longer stalled by a barrier then wake it.
synchronized (this) {
Message prev = null;
Message p = mMessages;
while (p != null && (p.target != null || p.arg1 != token)) {
prev = p;
p = p.next;
}
if (p == null) {
throw new IllegalStateException("The specified message queue synchronization "
+ " barrier token has not been posted or has already been removed.");
}
final boolean needWake;
if (prev != null) {
prev.next = p.next;
needWake = false;
} else {
mMessages = p.next;
needWake = mMessages == null || mMessages.target != null;
}
p.recycleUnchecked();
// If the loop is quitting then it is already awake.
// We can assume mPtr != 0 when mQuitting is false.
if (needWake && !mQuitting) {
nativeWake(mPtr);
}
}
}
有添加就有移除,必定是成对出现的,否则的话队列后⾯的消息永远没法得到处理,消息机制就崩了,这个后⾯可以验证⼀下.
quit
void quit(boolean safe) {
if (!mQuitAllowed) {
throw new IllegalStateException("Main thread not allowed to quit.");
}
synchronized (this) {
if (mQuitting) {
return;
}
//将标记置为true
mQuitting = true;
if (safe) {
//如果是安全退出,则移除msg.when⼤于当前时间点的所有消息
removeAllFutureMessagesLocked();
} else {
//移除所有的消息
removeAllMessagesLocked();
}
// We can assume mPtr != 0 because mQuitting was previously false.
//唤醒阻塞,退出looper循环
nativeWake(mPtr);
}
}
退出当前消息队列,有两种模式,安全和⾮安全
安全:移除当前时间点之后的所有消息
不安全:移除队列中的所有消息
removeCallbacksAndMessages
根据Handler移除队列中的消息和回调,都是常规的链表操作,就不贴代码了
Message
之前提到过最多的就是它了,现在来详细了解⼀下
官⽅说明
Defines a message containing a description and arbitrary data object that can be
sent to a {@link Handler}. This object contains two extra int fields and an
extra object field that allow you to not do allocations in many cases.
⾕歌翻译:定义⼀条消息,其中包含可以发送到{@link Handler}的描述和任意数据对象。
此对象包含两个额外的int字段和⼀个 extra object字段,允许您在许多情况下不进⾏分配。
构造函数
/** Constructor (but the preferred way to get a Message is to call {@link #obtain() Message.obtain()}).
*/
public Message() {
}
默认的构造⽅法,不做任何操作,但是注释告诉我们更好的获取Message实例的⽅法是调⽤Message.obtain()
为什么呢?那看⼀下obtain⽅法的实现
核⼼⽅法
obtain
/**
* 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();
}
内部使⽤链表结构维护了⼀个消息池,每次取链头节点,如果链表为空,新创建⼀个消息返回.
再看⼀下回收⽅法
recycle
/**
* Return a Message instance to the global pool.
* <p>
* You MUST NOT touch the Message after calling this function because it has
* effectively been freed. It is an error to recycle a message that is currently
* enqueued or that is in the process of being delivered to a Handler.
* </p>
*/
public void recycle() {
if (isInUse()) {
if (gCheckRecycle) {
throw new IllegalStateException("This message cannot be recycled because it "
+ "is still in use.");
}
return;
}
recycleUnchecked();
}
/**
* Recycles a Message that may be in-use.
* Used internally by the MessageQueue and Looper when disposing of queued Messages.
*/
void recycleUnchecked() {
// Mark the message as in use while it remains in the recycled object pool.
// Clear out all other details.
//重置参数
flags = FLAG_IN_USE;
what = 0;
arg1 = 0;
arg2 = 0;
obj = null;
replyTo = null;
sendingUid = -1;
when = 0;
target = null;
callback = null;
data = null;
//将当前消息添加到链表头部
synchronized (sPoolSync) {
if (sPoolSize < MAX_POOL_SIZE) {
next = sPool;
sPool = this;
sPoolSize++;
}
}
}
逻辑很简单,重置各种成员变量,然后添加到链表头部.
需要注意的是,我们并不需要⼿动去调⽤recycle⽅法,在消息被消费掉的时候,Looper内部⾃动为我们调⽤了.为什么需要消息池呢?每次创建⼀个会产⽣什么问题呢?
嗯,频繁地新建和销毁对象会造成频繁gc,内存抖动以及界⾯卡顿.
到此为⽌,消息创建,分发,消费,回收整个流程都分析完毕,那么看了这么多源码到底有哪些收获呢?
⾄少可以清晰地回答以下问题:
q1: Android消息机制是什么?涉及哪些主要的类?
q2: MessageQueue是基于哪⼀个数据结构实现的,有何特点?
q3: 为什么Message中when使⽤SystemClock.uptimeMillis()?
q4: 什么是屏障消息? 怎么向消息队列添加⼀个屏障消息? 以及屏障消息的作⽤?
q5: 如果添加闲时任务到消息队列?
q6: 如何参照Message回收机制构建⼀个对象回收池?
q7: 如何在⼦线程创建Handler实例?
q8: 如何利⽤Looper构建⼀个消息循环系统?
q9: 什么情况下会发⽣内存抖动,界⾯卡顿?
差不多了,⼤概就这些吧?最后附上⼀张⽹络上盗来的图(其实是⾃⼰不会画)
第⼀次写这么长的源码分析⽂章,且看且珍惜吧!。