Handler.post (runnable) does not always execute the run method in android

I created an instance of Handler in the main thread ui (mUIHandler) and from the worker thread (another thread), when I try to execute the run runable method, the run method runs almost 9 out of 10 times, but there is 1 time when it is not running.

mUIHandler.post (uiRunnable) → Does not always guarantee the execution of the launch method present in runnable?

I even added logging methods for verification and could see that the logs until the innopocation method was run, but the execution logs were not displayed.

How does mail (runnable) work inside? Does this guarantee that the ui thread (the thread with the handler) will emit this as soon as post is called?

Any help would be appreciated.

Thank!

+5
source share
2 answers

I have never seen a handler not execute a running runnable. Some things to learn:

  • Is there any logic that could lead to a race condition between the data that the thread could potentially interact while the UI thread executable is executing?
  • Do you have any attempt / catch anywhere where you could safely have an exception?

My vote (without your code) is that it is probably No. 1. You would not be the first person to become a victim of the harsh conditions of the race as a result of parallel logic.

+6
source

I ran into this problem on Android 2.2, in my case both Runnables and Messages were used with the same handler.

Handler, , "" 0 . , Handler Runnable "what", removeMessages(0). .

+10

All Articles