MessageQueue
MessageQueue.IdleHandler
IdleHandler是MessageQueue内部的一个接口,他是一个空闲处理程序。
就是一个在MessageQueue获取不到Message消息时执行的接口。
我们看看MessageQueue空闲时执行的代码:
Message next() { int pendingIdleHandlerCount = -1; // -1 only during first iteration synchronized (this) { //... //没有消息时退出队列 if (mQuitting) { dispose(); return null; } 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); } 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; } }
我去掉了获取Message相关的代码,保留了和IdleHandler相关的逻辑。
上面这段代码很简单,就是判断mIdleHandlers队列是不是空,如果为空就进入等等状态。不为空表示有地方实现了IdleHandler接口。遍历数组内的所有对象,执行空闲处理代码
执行IdleHandler的几个前置条件是:
- 当链表中没有得到需要执行的Message时(消息队列为空或者队列中的Message没有到执行时间)
- 设置了队列获取消息为空不退出队列
- 实现了IdleHandler接口,并调用
addIdleHandler()
添加
IdleHandler的应用场景
ActivityThread内定义了三个内部类,分别实现是:
- Idler
- GcIdler
- PurgeIdler
他们都实现了IdleHandler接口。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律