【问题标题】:Platform.runLater() - how to run multiple runnable element and get valuePlatform.runLater() - 如何运行多个可运行元素并获取价值
【发布时间】:2019-06-26 20:39:44
【问题描述】:

我需要在“串行”中执行 3 个可运行实例,换句话说,在同一个线程中,一个接一个地执行,并得到结果。

目前我的代码是:

SendCommandTask sendLogReadCommand = new SendCommandTask(methodNameGetLog);
    sendLogReadCommand.setOnSucceeded(eventLog -> {
        this.LogTemp = ((List<Log>) sendLogReadCommand.getValue()).get(0);
        SendCommandTask sendSpaceReadCommand = new SendCommandTask(methodNameGetSpace);
        sendSpaceReadCommand.setOnSucceeded(eventSpace -> {
            this.boxTemp = ((List<BoxLabellTs>) sendSpaceReadCommand.getValue()).get(0);
            SendCommandTask sendAllLabellTssReadCommand = new SendCommandTask(methodNameGetAllLabellTss);
            this.listLabellTssTemp = (List<BotLabellTs>) sendAllLabellTssReadCommand.getValue();
            this.composeBox();              
        });
    });
    Platform.runLater(sendLogReadCommand);

有没有办法只用一个 Platform.runLater 命令运行所有任务并获得每个任务的结果?

【问题讨论】:

  • Task 的全部意义在于提供一些从后台线程到 JavaFX 应用程序线程的属性更改的方法。 Platform.runLater 在 JavaFX 应用程序线程上运行任务; onSucceeded 处理程序也在 JavaFX 应用程序线程上运行。您可以通过调用run() 方法在JavaFX 应用程序线程上一个接一个地执行所有任务,并达到相同的效果。
  • call() 的实现中,调用updateValue() 来指示部分结果;在 GUI 上,监听 value 属性的变化,如 here 所示。
  • 我有一种感觉你想要它反过来:创建一个线程,你是否在你自己的线程中长时间运行东西,并在你想更新 gui 时从该线程调用 Platform.runlater() (仅在 fx 线程中运行图形的东西)

标签: java user-interface javafx task


【解决方案1】:

找到了! this link solve my problem:

下面的代码很干净并且可以正常工作!

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);}

也可以这样用:

final Timeline timeline1 = new Timeline(kf1); final Timeline timeline2 = new Timeline(kf2); final Timeline timeline3 = new Timeline(kf3); SequentialTransition sequence = new SequentialTransition(timeline1, timeline2, timeline3); Platform.runLater(sequence::play);

【讨论】:

    猜你喜欢
    • 2017-09-08
    • 2021-02-01
    • 1970-01-01
    • 2018-04-21
    • 2021-10-26
    • 1970-01-01
    • 2015-05-31
    • 2023-04-10
    • 2017-08-16
    相关资源
    最近更新 更多