4

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.

Community
  • 1
  • 1
committedandroider
  • 8,711
  • 14
  • 71
  • 126

1 Answers1

9

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.

iagreen
  • 31,470
  • 8
  • 76
  • 90
  • Thanks but I think an example would clear it up a lot more. Say I call postDelayed(new Runnable() { .... } , 50000). This would go in front of the message queue initially because there is nothing else inside the message queue. If I add another runnable, say post(new Runnable() {...}), will that runnable have to wait 50 seconds because of the "blocking operation" of the first post because the first post has to wait that long to execute? – committedandroider Dec 02 '14 at 04:06
  • No, the order of the queue is the determined by the time associated with the message. For non-delayed messages, this is the current time. For delayed messages, it is the current time plus the delay. Thus, 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. Updated my answer to provide this detail. – iagreen Dec 02 '14 at 04:10