在Platform.runLater可运行之后添加延迟 [英] Add delay after Platform.runLater runnable

查看:205
本文介绍了在Platform.runLater可运行之后添加延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是javaFX的初学者,可能我错过了一些东西,但是这里是我的问题:我应该按顺序在gui应用程序中显示一些更改。



我的代码看起来像这样(每个方法都会在gui中进行更改,但是最终结果当然是由最后一个方法给出的):

  public void updateGui()
{
Platform.runLater(()-> {
doFirstStuff();
doSecondStuff();
doThirdStuff();
});
}

如果在每种方法的执行之间有一点延迟,那会很好(1,2或3秒),让用户有时间查看应用程序中发生的情况。



我的尝试是:

  Thread.sleep(1000); 

在每个方法调用之后,但是由于线程是JavaFX Application Thread,因此结果很糟糕。 / p>

我现在想到的另一个解决方案是:



使用Runnable在另一个线程中执行每个方法像这样:

 ()-> {
Platform.runLater(()-> {
doFirstStuff();
});
Thread.sleep(1000);
});

并在 updateGui()中提交任务在 newSingleThreadExecutor()中,等待每个任务的终止。但是我不知道,这似乎不是一个好的解决方案。



实现我的目标的最佳方法是什么?

解决方案

最简单的方法就是使用 时间轴

  public void updateGui(){
final KeyFrame kf1 = new KeyFrame(Duration.seconds(0),e-> doFirstStuff());
final KeyFrame kf2 = new KeyFrame(Duration.seconds(1),e-> doSecondStuff());
final KeyFrame kf3 = new KeyFrame(Duration.seconds(2),e-> doThirdStuff());
最终时间轴时间轴=新时间轴(kf1,kf2,kf3);
Platform.runLater(timeline :: play);
}

我在这里假设 updateGui()在后台线程上被调用(否则只需删除 Platform.runLater(...)并调用 timeline.play() 直接)。



如果您真的想弄脏线程,则可以(或多或少)达到相同的效果

  public void updateGui(){

线程thread = new Thread(()-> {
try {
Platform.runLater(()-> doFirstStuff());
Thread.sleep(1000);
Platform.runLater(()-> doSecondStuff()) ;
Thread.sleep(1000);
Platform.runLater(()-> doThirdStuff());
} catch(InterrupedException exc){
//不应该能够到达这里...
抛出新的Error(意外中断);
}
};
thread.start();
}

b我想对于这样的用例来说, Timeline (或动画API的其他部分)更干净(并且使用更少的资源,因为它们不会创建新的线程)。


I'm really beginner with javaFX and probably I'm missing something, but here my issue: I should show some changes in my gui application sequentially.

My code looks like this (each method performs a change in the gui, but the final result is given by the last method of course):

public void updateGui()
{
    Platform.runLater(() -> {
        doFirstStuff();
        doSecondStuff();
        doThirdStuff();
    });
}

It would be nice if between the execution of each method there was a little delay (1,2 or maybe 3 seconds) for give time to the user to see what happened in the application.

My attempt was put:

Thread.sleep(1000);

after each method call, but since the thread is the JavaFX Application Thread, the result is awful.

Another solution that comes up right now in my mind is:

execute each method in another thread with a Runnable like this:

() -> {
    Platform.runLater(() -> {
        doFirstStuff();
    });
    Thread.sleep(1000);
});

and in updateGui() submit the task in a newSingleThreadExecutor() and wait for the termination of each task. But I don't know, it doesn't seem a good solution.

What is the best way to achieve my goal?

解决方案

The simplest way is just to use a Timeline:

public void updateGui() {
    final KeyFrame kf1 = new KeyFrame(Duration.seconds(0), e -> doFirstStuff());
    final KeyFrame kf2 = new KeyFrame(Duration.seconds(1), e -> doSecondStuff());
    final KeyFrame kf3 = new KeyFrame(Duration.seconds(2), e -> doThirdStuff());
    final Timeline timeline = new Timeline(kf1, kf2, kf3);
    Platform.runLater(timeline::play);
}

I am assuming here that updateGui() is being called on a background thread (otherwise just remove Platform.runLater(...) and call timeline.play() directly).

If you really want to get your hands dirty with threads, you could achieve the same (more or less) with

public void updateGui() {

    Thread thread = new Thread(() -> {
        try {
            Platform.runLater(() -> doFirstStuff());
            Thread.sleep(1000);
            Platform.runLater(() -> doSecondStuff());
            Thread.sleep(1000);
            Platform.runLater(() -> doThirdStuff());
        } catch (InterrupedException exc) {
            // should not be able to get here...
            throw new Error("Unexpected interruption");
        }
    };
    thread.start();
}

but I think for use cases like this, the Timeline (or other parts of the animation API) are much cleaner (and use fewer resources, since they don't create a new thread).

这篇关于在Platform.runLater可运行之后添加延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆