LeakCanary 中的 IdleHandler简介

这个类的作用是 在 主线程没有可处理的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 则只走一次

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值