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:
- is executed once task has finished
- has access result of execution of background task, via
task.getvalue()
- 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
Post a Comment