简体   繁体   中英

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 worker thread finishes. Could anybody please suggest me a solution?

{
    Main thread...
    worker thread created..and executed..
    //I need to wait here for the worker thread to finish//
    some useful task to be done
}

Thanks in advance!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM