- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在查看 postDelayed 的 Android 文档 post delayed documentation
这类似于另一个问题 - https://stackoverflow.com/questions/25820528/is-postdelayed-relative-to-when-message-gets-on-the-queue-or-when-its-the-actual - 我有一段时间了,但情况不同(而且在我脑海中措辞更清晰)
基本上这就是文档对这个方法所说的内容 - “使 Runnable 添加到消息队列,在指定的时间过去后运行。Runnable 将在用户界面线程上运行。”
我知道每个线程都有一个与之关联的消息队列、循环器和处理程序。 - What is the relationship between Looper, Handler and MessageQueue in Android? .就“在指定的时间过去后运行”而言,如果您传入 0 作为 delayMillis 的参数并且消息队列中仍有消息,则带有 0 的消息会跳过其余消息(即当前在它的前面)在消息队列中由looper直接处理?我知道 looper 会将消息发送到 Handler 的 handleMessage() 方法 - 来自 How the Looper knows to send the message to Handler? .我会自己测试这个,但我真的不知道你会怎么做。
最佳答案
简短的回答是 - 不,执行 postDelayed
不会跳到队列中其他非延迟作业的前面。
post
和postDelayed
都调用了sendMessageDelayed
,post
使用了0的延迟。因此,post
和 postDelayed
具有零延迟是等效的。 (参见 Handler
source ,从第 324 行开始)。 sendMessageDelayed
说明消息在所有未决请求之后放入队列。原因是每条消息都在排队时间加上一个可选的延迟时间。队列按此时间值排序。如果您没有延迟地排队新消息,它将跳过(放在前面)仍未到达其传递时间的延迟消息,但不会在未决消息(那些已经过了传递时间但尚未到达的消息)前面交付)
作为旁注,如果您想要请求跳过待处理请求的行为,您可以使用postAtFrontOfQueue
,但请务必阅读并理解仅在特殊情况下使用的警告。
关于java - postDelayed 是否导致消息跳到队列的前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27240015/
我的 JavaScript 代码中有一道数学题。我需要将给定数量的玩家随机分成 2 队,这样每次 - 如果玩家想再次比赛 - 团队都会重新组成,并且在形成所有组合之前他们应该不同。 假设我有 4 个玩
我是一名优秀的程序员,十分优秀!