简体   繁体   中英

How do I make the current thread wait for another thread to finish before proceeding?

I've dispatched a new thread to handle some execution related to a Swing GUI. However, once this thread accomplishes its task, all execution stops. Specifically, in the code below, the final print statement is not being executed. Why is this so? How can I continue execution after the new Thread completes its operations? If this is a dumb question, any multithreading resource recommendations?

Also, just a little bit of background: my program includes both JavaFX and Swing GUIs, which is why the first thread is a JavaFX thread. I'm currently trying to update the Swing GUI, and subsequently continue execution.

Thanks for the help.

  System.out.println("Thread: " + Thread.currentThread());
  //prints "Thread : [JavaFX Application Thread,5,main]"
  new Thread(new Task<Void>(){
    @Override protected Void call() throws Exception{
      System.out.println("Thread: " + Thread.currentThread());
      //prints "Thread : [Thread-6,5,main]"
      updateData();
      updateSwingGUI();
      return null;
    }
  }).start();
  System.out.println("I'm not being executed");

If you want to block your current thread until the other thread finishes its task, maybe doing the task on a separate thread is actually pointless, you are not achieving any parallelism.

Your Task isn't even returning anything, so there is even no other reason to block until a result from the second thread is returned.

If you still want to execute it in parallel and block waiting for the task to finish, you could use this:

System.out.println("Thread: " + Thread.currentThread());
Thread thread = new Thread(() -> {
      System.out.println("Thread: " + Thread.currentThread());
      updateData();
      updateSwingGUI();
   }).start();

System.out.println("I'm on the old thread, can do something in parallel.");

thread.join();
System.out.println("Second thread finished updating GUI.");

That println() is running on the original thread: once you've started the new thread, unless you use some sort of "coordination" between the threads, they will run concurrently and there is no way to guess about the relative order of statements in different threads.

If you indeed want to wait for that first thread to finish before running the println() try something like:

System.out.println("Thread: " + Thread.currentThread());
//prints "Thread : [JavaFX Application Thread,5,main]"

// Note that we keep a reference to the thread we created.
Thread t = new Thread(new Task<Void>(){
  @Override protected Void call() throws Exception{
      System.out.println("Thread: " + Thread.currentThread());
      //prints "Thread : [Thread-6,5,main]"
      updateData();
      updateSwingGUI();
      return null;
   }
}).start();
System.out.println("I'm not being executed");
t.join();
// We then have the original thread "join" on the thread via the reference.

That join() causes the thread executing that statement to wait for the Thread t to finish before proceeding past the join() .

That said, if you have a set of threads, but they are only executing one at a time (as like this example), you defeat the whole purpose of threads - which is to allow for concurrent execution of different tasks.

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