Java tutorial
//package com.java2s; import javafx.application.Platform; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; public class Main { private static final Semaphore uiSemaphore = new Semaphore(1); private static final ExecutorService singleExecutorService = Executors.newSingleThreadExecutor(); public static void runActionLater(final Runnable runnable) { if (Platform.isFxApplicationThread()) { runnable.run(); } else { try { uiSemaphore.acquire(); Platform.runLater(() -> { try { runnable.run(); releaseUiSemaphor(); } catch (Exception e) { releaseUiSemaphor(); throw new RuntimeException(e); } }); } catch (Exception e) { releaseUiSemaphor(); throw new RuntimeException(e); } } } public static void runActionLater(Runnable runnable, boolean force) { if (force) { Platform.runLater(runnable); } else { runActionLater(runnable); } } private static void releaseUiSemaphor() { singleExecutorService.submit(() -> { uiSemaphore.release(); }); } }