JavaFX暂停我的主线程

时间:2018-07-30 06:49:43

标签: java multithreading javafx

我有自己的线程,需要时我用JavaFX激活GUI,那时我的程序线程暂停或停止

public static void main(String[] args) {
    try {
    loader.init();      
    loader.finish();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我的初始化方法

    public void init() {
    if(!WebUtil.serverReacheable()) {
        System.out.println("Server is down, please try soon");
        return;
    }
    Preconditions.checkState(!serviceLoader.isShutdown(), "The bootstrap has been bound already!");
    executeServiceLoad();
    serviceLoader.shutdown();
    System.out.println("Initializing application...");
}

在哪里启动GUI

    private void executeServiceLoad() {
    serviceLoader.execute(() ->Application.launch(MaterialLogin.class));
}

我的执行人

private final ExecutorService serviceLoader = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("GameLoadingThread").build());
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat("GameThread").build());

1 个答案:

答案 0 :(得分:0)

由于@KDM,我为JavaFx接口创建了一个新的线程执行器

    private final ExecutorService guiExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("GuiThread").build());

每当我需要调用其他线程的runnable时,都需要这样调用Application.runLater()

                    Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        openDashBoard(response.body());

                    }

                });