Handler原理简述
只做简单概述,具体详细细节可查看相关源码
1.handler发送消息根据时间先后顺序插入到消息队列
public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis) {
if (delayMillis < 0) {
delayMillis = 0;
}
//根据消息具体的执行时间点排序
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
2.Looper.loop()不断从消息队列中取消息
for (;;) {
//不断从消息队列中取消息 queue.next()会阻塞
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
...
...
//取消消息后回调 handler分发消息 msg.target是发送消息的handler
msg.target.dispatchMessage(msg);
...
...
}
}
3.MessageQueue.next() 取消息,重点分析
Message next() {
//mPtr 相当于一把锁
final long ptr = mPtr;
if (ptr == 0) {
return null;
}
int pendingIdleHandlerCount = -1; // -1 only during first iteration
//距离最近的消息需要等待的时间
int nextPollTimeoutMillis = 0;
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
//等待 nextPollTimeoutMillis
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;
//重点 如果消息头是一个屏障消息(标志:msg.target == null)
if (msg != null && msg.target == null) {
// Stalled by a barrier. Find the next asynchronous message in the queue.
//查找下一个 msg.isAsynchronous() = true 的消息,如果没有 msg =null
//将会等待进入休眠状态,不会处理msg.isAsynchronous() = false的消息
//所以当消息头为屏障消息是,只会处理异步消息,之后的同步消息和屏障消息都不处理
//如果没有异步消息将会进入休眠状态
do {
prevMsg = msg;
msg = msg.next;
} while (msg != null && !msg.isAsynchronous());
}
//msg != null 说明头部不是屏障头,或者是屏障消息头且找到了异步消息
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;
}
····
····
}
}
4.简单总结一下
1.消息队列中可存放三种消息,屏障消息(msg.target == null),异步消息(msg.isAsynchronous()= true),同步消息(msg.isAsynchronous()= false)
2.如果消息头不是屏障消息,则异步消息和同步消息没有区别,消息队列头会后移
3.如果消息头是屏障消息,则只能通过异步消息,不能通过同步消息,消息队列头不会后移,只能手动移除
发送三种消息
1.发送普通消息,当然handler可指定默认消息类型
//构造方法 async
public Handler(boolean async) {
this(null, async);
}
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);
}
//如果handler mAsynchronous = ture ,设置message的isAsynchronous= false无效,
//此handler发送的消息都是异步消息
val obtainMessage = handler.obtainMessage().apply {
what = 1
isAsynchronous = false
obj = "普通消息”
}
handler.sendMessageDelayed(obtainMessage, time)
2.发送异步消息
val obtainMessage = handler.obtainMessage().apply {
what = 2
isAsynchronous = true
obj = "异步消息”
}
handler.sendMessageDelayed(obtainMessage, time)
3.发送屏障消息,同步异步消息只有在遇到屏障头消息时才有效,查看源码
/*
* @hide
*/
@TestApi
public int postSyncBarrier() {
return postSyncBarrier(SystemClock.uptimeMillis());
}
// 注意:when是计算后的时间
//可以看出屏障消息的特点是msg.target == null,且msg.isAsynchronous() = false
private int postSyncBarrier(long when) {
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;
}
}
4.发送屏障消息,反射调用
//获取 发送屏障消息postSyncBarrier函数
private val postSyncBarrier = MessageQueue::class.java.getDeclaredMethod("postSyncBarrier", Long::class.java).apply {
isAccessible = true
}
//获取 撤销屏障消息 removeSyncBarrier函数
private val removeSyncBarrier = MessageQueue::class.java.getDeclaredMethod("removeSyncBarrier", Int::class.java).apply {
isAccessible = true
}
使用Demo
//初始化handler
private val handler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
Log.d(TAG, "handleMessage:-${msg.what}--${msg.isAsynchronous}-- ${msg.obj}--${msg.`when`}")
when (msg.what) {
0 -> {
//收到消息后异常屏障,注意只能用异步消息移除屏障
removeSyncBarrier.invoke(queue, msg.g1)
}
}
}
}
//获取当前队列
private val queue = handler.looper.queue
//获取队列的postSyncBarrier函数
private val postSyncBarrier = MessageQueue::class.java.getDeclaredMethod("postSyncBarrier", Long::class.java).apply {
isAccessible = true
}
//获取队列的removeSyncBarrier函数
private val removeSyncBarrier = MessageQueue::class.java.getDeclaredMethod("removeSyncBarrier", Int::class.java).apply {
isAccessible = true
}
private fun senMsg(handler: Handler, w: Int, async: Boolean, str: String, time: Long, arg: Int = 0) {
val obtainMessage = handler.obtainMessage().apply {
what = w
arg1 = arg
isAsynchronous = async
obj = str
}
handler.sendMessageDelayed(obtainMessage, time)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
senMsg(handler, 1, false, "同步消息", 1000)
//发送屏障消息 返回屏障标识token
val token = postSyncBarrier.invoke(queue, SystemClock.uptimeMillis() + 2000)
senMsg(handler, 2, false, "同步消息", 3000)
senMsg(handler, 3, false, "同步消息", 4000)
senMsg(handler, 4, true, "isAsynchronous 异步消息 ", 5000)
senMsg(handler, 5, false, "同步消息", 6000)
senMsg(handler, 6, true, "isAsynchronous 异步消息", 7000)
senMsg(handler, 7, false, "同步消息", 8000)
senMsg(handler, 8, true, "isAsynchronous 异步消息", 9000)
//发送0 移除屏障token
senMsg(handler, 0, true, "isAsynchronous 移除屏障", 9000, token as Int)
senMsg(handler, 9, false, "同步消息", 10 * 1000)
senMsg(handler, 10, false, "同步消息", 11 * 1000)
senMsg(handler, 11, true, "isAsynchronous 异步消息", 12 * 1000)
senMsg(handler, 12, false, "同步消息", 13 * 1000)
senMsg(handler, 13, false, "同步消息", 14 * 1000)
senMsg(handler, 11, true, "isAsynchronous 异步消息", 15 * 1000)
}
测试结果
//1 消息后是一个屏障消息
19:54:37.854 : handleMessage:-1--false-- 同步消息--11075427
//只处理异步消息
19:54:41.857 : handleMessage:-4--true-- isAsynchronous 异步消息 --11079427
19:54:43.855 : handleMessage:-6--true-- isAsynchronous 异步消息--11081427
19:54:45.855 : handleMessage:-8--true-- isAsynchronous 异步消息--11083427
//收到0后 会移除屏障消息
19:54:45.855 : handleMessage:-0--true-- isAsynchronous 移除屏障--11083427
//继续按照队列的顺序正常执行
19:54:45.855 : handleMessage:-2--false-- 同步消息--11077427
19:54:45.856 : handleMessage:-3--false-- 同步消息--11078427
19:54:45.856 : handleMessage:-5--false-- 同步消息--11080427
19:54:45.856 : handleMessage:-7--false-- 同步消息--11082427
19:54:46.854 : handleMessage:-9--false-- 同步消息--11084427
19:54:47.854 : handleMessage:-10--false-- 同步消息--11085427
19:54:48.854 : handleMessage:-11--true-- isAsynchronous 异步消息--11086427
19:54:49.854 : handleMessage:-12--false-- 同步消息--11087427
19:54:50.855 : handleMessage:-13--false-- 同步消息--11088428
19:54:51.855 : handleMessage:-11--true-- isAsynchronous 异步消息--11089428
扩展:Looper.myQueue().addIdleHandler(mPurgeIdler);
添加空闲任务 MessageQueue#addIdleHandler(@NonNull IdleHandler handler)
//添加空闲任务
public void addIdleHandler(@NonNull IdleHandler handler) {
if (handler == null) {
throw new NullPointerException("Can't add a null IdleHandler");
}
synchronized (this) {
mIdleHandlers.add(handler);
}
}
消息队列没有消息时会执行mIdleHandlers任务
Message next() {
//...
//...
//mMessages队列没有消息时
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);
}
//任务是否持续执行
if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}
//...
//...
}