run And Wait Thread in JavaFX Thread - Java JavaFX

Java examples for JavaFX:JavaFX Thread

Description

run And Wait Thread in JavaFX Thread

Demo Code


//package com.java2s;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import javafx.application.Platform;

public class Main {
    public static void runAndWait(Runnable runnable)
            throws InterruptedException, ExecutionException {
        if (Platform.isFxApplicationThread()) {
            try {
                runnable.run();/*from   ww  w. ja va2  s  .  c  om*/
            } catch (Exception e) {
                throw new ExecutionException(e);
            }
        } else {
            FutureTask<Object> futureTask = new FutureTask<>(runnable, null);
            Platform.runLater(futureTask);
            futureTask.get();
        }
    }

    public static void runLater(Runnable runnable) {
        Platform.runLater(runnable);
    }
}

Related Tutorials