JavaFx platform.runlater 반환값, 작업 반환 대기
3649 단어 Javafx
// FutureTask, Plateform.runLater() futuretask
final FutureTask query = new FutureTask(new Callable() {
@Override
public String call() throws Exception {
// ( stage)
VcodeController vc = new VcodeController();
return vc.show(url4vcode);
}
});
Platform.runLater(query); //
String vcode = query.get(); //
System.out.println(vcode);
CountDownLatch를 활용하는 또 다른 방법
/**
* Runs the specified {@link Runnable} on the
* JavaFX application thread and waits for completion.
*
* @param action the {@link Runnable} to run
* @throws NullPointerException if {@code action} is {@code null}
*/
public static void runAndWait(Runnable action) {
if (action == null)
throw new NullPointerException("action");
// run synchronously on JavaFX thread
if (Platform.isFxApplicationThread()) {
action.run();
return;
}
// queue on JavaFX thread and wait for completion
final CountDownLatch doneLatch = new CountDownLatch(1);
Platform.runLater(() -> {
try {
action.run();
} finally {
doneLatch.countDown();
}
});
try {
doneLatch.await();
} catch (InterruptedException e) {
// ignore exception
}
}