0

I need to execute 3 runnable instance in "serial" in other word, one by one but in the same thread, and get the result.

Currently my code is:

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

Is there a way to run all with only one Platform.runLater command and get the result of each task?

rugby82
  • 679
  • 1
  • 9
  • 25
  • 1
    The whole point of `Task` is to provide some ways getting some property changes from a background thread to the JavaFX application thread. `Platform.runLater` runs the task on the JavaFX application thread though; the `onSucceeded` handlers also run on the JavaFX application thread. You could simply execute all the tasks one after the other on the JavaFX application thread by calling the `run()` method and achieve the same effect. – fabian Feb 02 '19 at 14:41
  • 1
    In your implementation of `call()`, invoke `updateValue()` to indicate partial results; on the GUI, listen for the changing `value` property, as shown [here](https://stackoverflow.com/a/44056730/230513). – trashgod Feb 02 '19 at 17:20
  • I have a feeling you want it the other way round: create a Thread, do you longrunning stuff in your own thread and from that thread you call Platform.runlater() whenever you want to update the gui(only run the graphical stuff in the fx thread) – kai Feb 04 '19 at 12:08

1 Answers1

0

Found! this link solve my problem:

the code below is clean and it works!

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

could be used also this:

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

rugby82
  • 679
  • 1
  • 9
  • 25