From 4dc96160d49b06015794ebc89f1ac029a9d7ef3b Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Mon, 30 Mar 2020 15:18:48 +0800 Subject: [PATCH] =?UTF-8?q?Create=20Handler=20=E6=B6=88=E6=81=AF=E5=BB=B6?= =?UTF-8?q?=E6=97=B6=E5=AE=9E=E7=8E=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../线程通信相关/Handler 消息延时实现.md | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 blogs/Android/Framework/Interview/线程通信相关/Handler 消息延时实现.md diff --git a/blogs/Android/Framework/Interview/线程通信相关/Handler 消息延时实现.md b/blogs/Android/Framework/Interview/线程通信相关/Handler 消息延时实现.md new file mode 100644 index 0000000..9ba8f49 --- /dev/null +++ b/blogs/Android/Framework/Interview/线程通信相关/Handler 消息延时实现.md @@ -0,0 +1,63 @@ +--- +Handler 的消息延时是怎么实现的? +--- + +1. 消息延时是做了什么特殊处理? +2. 是发送延时了,还是消息延时处理了? +3. 延时精度怎么样? + +```java +boolean sendMessageDelayed(Message msg, long delayMillis){ + if(delayMillis<0){ + delayMillis=0; + } + return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis); +} +public boolean sendMessageAtTime(Message msg, long uptimeMillis){ + MessageQueue queue = mQueue; + return enqueueMessage(queue, msg, uptimeMillis); +} +boolean enqueueMessage(Message msg, long when){ + msg.when = when; + Message p = mMessage, prew; + if(p==null||when==0||when