Should postDelayed trigger the post to go to the queue?

I was looking at android docs for postDelayed post delayed docs

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 - mine was some time ago, but it was a different situation (and clearer in my mind)

This is basically what the docs say for this method - "Forces the Runnable to be added to the message queue, which will run after a specified amount of time. The Runnable will run on the UI 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 run after a specified amount of time", if you pass 0 as an argument to delayMillis and there are still messages in the message queue, will the message with 0 skip the rest of the messages (which are in front of it) in the message queue that will be processed with a looper? I know the looper will send a message to the Handler's handleMessage () method from How does Looper know how to send a message to the handler? ... I would have experienced this myself, but I really don't know how you would go about it.

+3


source to share


1 answer


The short answer is no, execution postDelayed

does not jump ahead of other jobs without queuing up.

Both post

and postDelayed

both calls sendMessageDelayed

, post

using the delay to 0. Thus, post

and postDelayed

with zero delay equivalent. (See Handler

source
starting at line 324). sendMessageDelayed

asserts that the message has been queued after all pending requests. This is because each message is queued with an on-time and optional delay. The queue is ordered by this time value. If you bring in a new message without delay, it will skip (be placed before) delayed messages that have not yet reached the delivery date, but not before the waiting messages (those that have passed the delivery time but have not yet been delivered)



As a side note, if you want a behavior to skip pending requests, you can postAtFrontOfQueue

, but be sure to read and understand, the warning that it should only be used in special circumstances.

+8


source







All Articles