Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does postDelayed cause the message to jump to the front of the queue?

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

This is similar to another question - https://stackoverflow.com/questions/25820528/is-postdelayed-relative-to-when-message-gets-on-the-queue-or-when-its-the-actual - I had a while back but its a different situation(and worded a lot clearer in my mind)

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."

I know that every thread has a message queue, looper and handler associated with it. - What is the relationship between Looper, Handler and MessageQueue in Android?. 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.

like image 835
committedandroider Avatar asked Jan 10 '23 08:01

committedandroider


1 Answers

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

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)

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.

like image 113
iagreen Avatar answered Jan 12 '23 02:01

iagreen