简体   繁体   中英

Continue only after thread finishes

I created New thread in anonymous class. How can I pause the program until the thread is finish. for check that everything works Ok I create while loop that dont continue but I know that this is not the solution. I look for the simplest solution.

 public void methodName() {
    Thread t = new Thread() {
        public void run() {
        }
    }
    t.start()
    System.out.print("Finish");
} 

I want that finish print only after the thread t finish.

In the thread I use an external library. And I discover that the thread finish but the function that I run inside the thread does not finish. Their is a way like Lock or Synchronize notify that I can stop the run until this function is finish?

I can put variable in the place when the run of the function is finished.

     ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
     query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() {
     public void done(ParseObject object, ParseException e) {
          if (e == null) {
           // object will be your game score
          } else {
          // something went wrong
           }
       }
       });

this is from the API. I want to put this Code in Thread and continue only after the done function is called. Thanks,

Just call join on the thread:

t.start();
t.join();
System.out.print("Finish");

The join call waits for the thread to terminate before returning.

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