I have created a Handler instance in the main ui thread(mUIHandler) and from a worker thread(other thread) when i am trying to execute the run method of the runnable the run method gets executed almost 9 out of 10 times but there is that 1 time when it doesn't get executed.
mUIHandler.post(uiRunnable) --> does it not always guarantee to execute the run method present in the runnable?
I even added log methods to check and could see that the logs till the post method innvocation gets executed but the run method logs don't get displayed.
How does the post(runnable) work internally? does it guarantee that the ui thread(thread with the handler) will excute this as soon as post is invoked?
Any help would be appreciated.
Thanks!
I've run into this problem as well on Android 2.2, in my case both Runnables and Messages were being used with the same Handler.
After looking at the Handler source code, it turns out that removing messages with a 'what' value of 0 also removes all queued Runnables. This happens because in the Handler class a Runnable is internally posted as a message with a 'what' value of zero, which are all removed by any call to removeMessages(0)
. Therefore, avoid using zero as message id.
I have never seen a Handler not properly run the posted runnable. Some things to investigate:
My vote (without having seen your code) is that it's probably #1. You wouldn't be the first person to fall victim to hard-to-track-down race conditions as a result of concurrent logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With