Thread.join()--Java implements the main thread waiting for the child thread

Thread.join() is used to specify that the current main thread waits for other threads to finish executing before continuing to execute the code behind Thread.join() .

String string = null;

Thread thread = new Thread(){
            public void run() {
try {
  string = query(username, pwd);
} catch (Exception e) {
e.printStackTrace ();
}
   };
};
thread.start();
thread.join();

if (string .equals("admin") )
{
return true;
}

The above piece of code is used for login verification, and query is used to query data from the server. Time-consuming operations cannot be placed on the main thread, and the if code segment used for judgment after a new sub-thread is executed is executed before the end of the sub-thread, so it cannot be judged. effect, so use the join function, so that you can wait for the child thread to finish executing before executing the judgment.

The join function can refer to http://blog.csdn.net/JustForFly/article/details/41118265

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325605135&siteId=291194637