简体   繁体   English

如何使当前线程在继续执行之前等待另一个线程完成?

[英]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. 我已经调度了一个新线程来处理与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. 而且,只有一点点背景知识:我的程序同时包含JavaFX和Swing GUI,这就是第一个线程是JavaFX线程的原因。 I'm currently trying to update the Swing GUI, and subsequently continue execution. 我当前正在尝试更新Swing GUI,然后继续执行。

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. 您的Task甚至没有返回任何内容,因此甚至没有其他原因可以阻塞,直到返回第二个线程的结果。

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. println()在原始线程上运行:一旦启动了新线程,除非在线程之间使用某种“协调”,否则它们将同时运行,并且无法猜测语句的相对顺序在不同的线程中。

If you indeed want to wait for that first thread to finish before running the println() try something like: 如果您确实想在运行println()之前等待第一个线程完成,请尝试以下操作:

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() . join()导致执行该语句的线程在继续通过join()之前等待线程t完成。

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. 就是说,如果您有一组线程,但它们一次只执行一个线程(如本例所示),则会破坏线程的全部目的-允许并发执行不同的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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