这个类的作用是 在 主线程没有可处理的message(.next 无message handle时)
会去判断 是否有IdleHandler 如果有 则调用IdleHandler接口的实现方法
使用场景:性能优化 不影响主线程的任务处理 ,当主线程空闲下来时 执行 类似pendingTask
实际应用: 在LeakCanary 源码中有发现, 因为LeakCanary需要GC来确认是否存在内存泄漏
而GC会阻塞线程 使用该 IdleHandler去走 GC及其GC后的泄漏确认流程
另外Glide 中也用到了
源码:
/**
* Callback interface for discovering when a thread is going to block
* waiting for more messages.
*/
public static interface IdleHandler {
/**
* Called when the message queue has run out of messages and will now
* wait for more. Return true to keep your idle handler active, false
* to have it removed. This may be called if there are still messages
* pending in the queue, but they are all scheduled to be dispatched
* after the current time.
*/
boolean queueIdle();
}
方法声明: 实现(queueIdle)时:
return true 则只要空闲就会走该实现方法
return false 则只走一次