java - Make JavaFX application thread wait for another Thread to finish -


i calling method inside ui thread. inside method new thread created. need ui thread wait until new thread finished because need results of thread continue method in ui thread. don´t want have ui frozen while waiting. there way make ui thread wait without busy waiting?.

you should never make fx application thread wait; freeze ui , make unresponsive, both in terms of processing user action , in terms of rendering physical screen.

if looking update ui when long running process has completed, use javafx.concurrent.task api. e.g.

somebutton.setonaction( event -> {      task<somekindofresult> task = new task<somekindofresult>() {         @override         public somekindofresult call() {             // process long-running computation, data retrieval, etc...              somekindofresult result = ... ; // result of computation             return result ;         }     }      task.setonsucceeded(e -> {         somekindofresult result = task.getvalue();         // update ui result     });      new thread(task).start(); }); 

obviously replace somekindofresult whatever data type represents result of long-running process.

note code in onsucceeded block:

  1. is executed once task has finished
  2. has access result of execution of background task, via task.getvalue()
  3. is in same scope place launched task, has access ui elements, etc.

hence solution can "waiting task finish", doesn't block ui thread in meantime.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -