不postDelayed导致消息跳跃到队列的前?队列、消息、postDelayed

2023-09-07 14:27:07 作者:打不赢就赶紧溜

我一直在寻找在Android文档的postDelayed 后推迟文档

I was looking on the Android docs for postDelayed post delayed documentation

这是类似于另一个问题 - Is postDelayed相对时,消息队列,或当其实际的消息轮到得到 - 我有一段时间回来,但它是一个不同的情况(在我的脑海里的措辞更加清晰)

This is similar to another question - Is postDelayed relative to when message gets on the queue or when its the actual message's turn - I had a while back but its a different situation(and worded a lot clearer in my mind)

基本上继承人什么文档说的,这种方法 - 时会加入到消息队列,时间经过指定金额后运行了Runnable的可运行将在用户界面线程中运行。

Basically heres what the docs say for that this method - "Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the user interface thread."

我知道,每个线程都有与之关联的消息队列,活套和处理程序。 - what是活套,处理程序和消息队列?之间的关系。 在时间经过指定金额后运行项,如果你传递0为delayMillis的参数,还有在消息队列中的消息,将与0的消息,跳过消息的其余部分(即目前在前面的话)在消息队列直接由弯针处理?我知道,弯针将分派消息处理程序的handleMessage()函数来自于方法How活套知道将消息发送给处理?。我会测试这个对我自己,但我真的不知道你将如何去做。

I know that every thread has a message queue, looper and handler associated with it. - what is the relationship between Looper, Handler and Message queue ?. In terms of "to be run after the specified amount of time elapses", if you pass in 0 as the argument for delayMillis and there are still messages in the message queue, will the message with 0 skip over the rest of the messages(that are currently in front of it) in the Message Queue to be directly handled by the looper? I know that looper will dispatch the message to the Handler's handleMessage() method- from How the Looper knows to send the message to Handler?. I would test this on my own but I don't really know how you would go about it.

推荐答案

简短的答案是 - 不,做了 postDelayed 不在其他非延迟的前跳队列中的作业。

The short answer is - no, doing a postDelayed does not jump in front of other non-delayed jobs in the queue.

两个 postDelayed 两个呼叫 sendMessageDelayed 采用0延迟。因此, postDelayed 与零延迟是等价的。 (请参阅 处理程序源,开始围绕行324)。 sendMessageDelayed 规定,所有挂起的请求后,放入队列中的消息。其原因是,每个消息被入队与它被添加到队列的时间加上一个可选延迟。该队列由该时间值排序。如果排队没有延迟它会跳过一个新的消息(放置在前面)推迟了还没有达到他们的交货时间的消息,但没有未决消息的前面(那些过去他们的交货时间,但还有待交付)

Both post and postDelayed both call sendMessageDelayed, post uses a delay of 0. Thus, post and postDelayed with a zero delay are equivalent. (See Handler source, starting around line 324). sendMessageDelayed states that the message in put in the queue after all pending requests. The reason is that each message is enqueued with the time it was enqueued plus an optional delay. The queue is ordered by this time value. If you enqueue a new message with no delay it will skip (be placed in front of) delayed messages that still have not reached their delivery time, but not in front of pending messages (those that are past their delivery time but have yet to be delivered)

作为一个侧面说明,如果你的需要的行为是请求跳过挂起的请求,你可以使用 postAtFrontOfQueue ,但一定要阅读并了解警告说,这只是在特殊情况下使用。

As a side note, if you want the behavior that a request skips pending requests, you can use postAtFrontOfQueue, but be sure to read and understand the warning that it only to be used in special circumstances.