From 521bb3cb6c2df817b9b626bccec79f86c91820c9 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Mon, 30 Mar 2020 16:08:28 +0800 Subject: [PATCH] =?UTF-8?q?Create=20IdleHandler=20=E5=8E=9F=E7=90=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../线程通信相关/IdleHandler 原理.md | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 blogs/Android/Framework/Interview/线程通信相关/IdleHandler 原理.md diff --git a/blogs/Android/Framework/Interview/线程通信相关/IdleHandler 原理.md b/blogs/Android/Framework/Interview/线程通信相关/IdleHandler 原理.md new file mode 100644 index 0000000..99508df --- /dev/null +++ b/blogs/Android/Framework/Interview/线程通信相关/IdleHandler 原理.md @@ -0,0 +1,87 @@ +--- +IdleHandler 原理 +--- + +1. 了解 IdleHandler 的作用以及调用方式 +2. 了解 IdleHandler 有哪些使用场景 +3. 熟悉 IdleHandler 的实现原理 + +```java +public static interface IdleHandler{ + boolean queueIdle(); +} +``` + +```java +Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler()){ + @Override + public boolean queueIdle(){ + return true; + } +} + +public void addIdleHandler(IdleHandler handler){ + synchronized(this){ + mIdleHandler.add(handler); + } +} +Message next(){ + for(;;){ + nativePollOnce(ptr, nextPollTimeoutMillis); + // 看消息列表是否有消息可以分发的,如果有,就返回该消息 + // 走到这说明没有消息可以分发,下一个 for 循环就要进入休眠了 + if(pendingIdleHandlerCount<0){ + pendingIdleHandlercount = mIdleHandlers.size(); + } + if(pendingIdleHandlerCount<0){ + continue; + } + mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers); + for(int i=0;i