Skip to content Skip to sidebar Skip to footer

Android:: Blocking Main Thread For Worker Thread To Finish Before Executing Other Tasks In Main Thread

I am executing some task in the UI thread and created a worker thread that does the networking stuff for me. However, I have something important that needs to be executed once the

Solution 1:

You need to do things on the UI thread after worker has finished? Use runOnUiThread at the end of your worker or use AsyncTask with onPostExecute(). Blocking the UI thread while worker is running doesn't make any sense.

Solution 2:

You don't. Blocking the UI thread violates the key rule of threading in Android. Doing so can cause those annoying "Application not responding" dialogs, along with other problems.

From the Processes and Threads page of the developer docs:

So, you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread. Thus, there are simply two rules to Android's single thread model:

Do not block the UI thread

Do not access the Android UI toolkit from outside the UI thread

You should explore other solutions to what you need to accomplish. You need not give specifics on your problem, but ekholm's solution could work, as could simply calling a method in your Activity from the worker thread.

Post a Comment for "Android:: Blocking Main Thread For Worker Thread To Finish Before Executing Other Tasks In Main Thread"