A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it — from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
http://developer.android.com/reference/android/os/Handler.html
Does this actually mean that the Runnable object will block the Messages posted after it?
The messages and Runnable objects actually in one queue?
The manual is little ambiguous to me.
Thanks!
It’s not so hard to check the code path of this. So in
Activity.javayou have the following code to passRunnableto Ui thread:Then for
Handler.post:Basically
sendMessageDelayedjust simply sends a message with delay 0 ms. So the most interesting part is how togetPostMessagefrom Runnable:So what does it mean? This means that the handler of the Ui thread if it gets message to run Runnable will try to execute it. And if this runnable operation is very long it will block the Ui thread that can cause ANR error.