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