简体   繁体   中英

How do you wait for a thread to finish in Java without freezing the GUI?

I am coding an android game in Eclipse with java. The main goal is to wait for a thread to finish, then to set a boolean to true. The reason is that when a user clicks a button it will only run if the boolean is true. However, when I call a method, it creates a thread and does its thing, then when it is done, it sets the boolean to true. However, it automatically sets the boolean to true while the thread is still running, and so the user can click the button (which messes some things up). Is there a way to wait for a thread to finish without freezing the screen? (thread.join() seems to be freezing it)

Any help is appreciated.

Seems like you don't really need to wait until the thread is done to continue, the way I see it you only need to be notified once it's done, the simplest approach for it would be passing a "callback listener" object to the thread, and execute it when done, this will let you know that you are ready to continue, OR a better approacch would be an AsyncTask which will allow you to do everything in background and when done you can use the onPostExecute method, hope this helps.

This is the way you create and add a callback to be notified when your thread has completed:

//Create your callback listener interface...
public interface MyThreadListener{
    public void threadFinished();
}

//This is the method that runs some functionality in a separate thread
public void executeTaskInSeparateThread(final MyThreadListener listener){
    new Thread(new Runnable() {

        @Override
        public void run() {
            // DO your long task here...

            //Notify when done before leaving run method...
            listener.threadFinished();
        }
    }).start();
}

//Use it like this
//Create the implementation of the listener you want (this is something like what you usually do for buttons or any other android listener)
MyThreadListener listener = new MyThreadListener() {

    @Override
    public void threadFinished() {
        //Do whatever you want when notified...
        //NOTE: This method will be called on a separated thread too, you cannot modify views...
    }
};
executeTaskInSeparateThread(listener);

Once the thread completed it will execute the listener and will let you know is done...

Regards!

Q: Why not set the button to "disabled" when you start the thread ( btn=.setEnabled(false); ), then have the thread set the button to "enabled" just before it exits?

And yes, calling "thread.join()" (or ANY blocking call) from your UI thread will indeed "freeze" it :)

PS: Are you using a Java thread, or an Android "Asynch Task"? Here's an excellent tutorial on the latter:

http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html

Use an AsyncTask . In onPreExecute() disable your button so the user cannot click it again. In onPostExecute method enable the button again. Do the time consuming logic in the doInBackground method.

try to create a Task extends AsyncTask, override the doinBackground mothed,Then put "time-consuming operation" in it. When your task done,it'll goto "onPostExecute",just use an Interface call back to the Actiity and enable the Button . When you use the AsyncTask you should know that: The Default AsyncTask has got a "pool",System allow 5 instace of AsyncTask ,if you got more than 5 Task,you should create a no limit pool.

My English is so so bad,lol.

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