Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Platform.runLater for multiple tasks in javaFX application

I have a JavaFX application which has several methods attached to it and there is one method like everything that calls all methods like

DFF
Lookup
Profile
Everything

Everything calls all these methods I am trying to implement threading on Everything methods to call these methods and then after the Lists are popuplated I transfer the control to another fxml to display it in table view using tab pane.

public void runEverything(){
    List<Task> tasks = new ArrayList();

    Task task = new Task() {
        protected Object call() throws Exception {                
            dataAOL = aol.getAolReport(InstanceController.validateto, InstanceController.validatefrom, pattern.getText());
            System.out.println("Method"+AOLScreenController.dataAOL.size());             

            return null;
        } 

    };
    Task task1 = new Task() {
        protected Object call() throws Exception {                
            dataProfile = aol.getAolReport(InstanceController.validateto, InstanceController.validatefrom, pattern.getText());
            System.out.println("Method"+AOLScreenController.dataAOL.size());                

            return null;
        } 

    };

    tasks.add(task);
    tasks.add(task1);

    ExecutorService threadPool = Executors.newFixedThreadPool(5);
    for (Task t : tasks) {               
        threadPool.submit(t);
    }
    threadPool.shutdown();
}

I need to call the Platform.runLater method only once after all threads have finished execution so that I load the fxml view and in the means while a progress bar will loop in the new fxml and ui will not lag . but I do not know where to place my Platform.runLater code

Platform.runLater(new Runnable() {
     @Override public void run() {
         try {
             Parent parent = FXMLLoader.load(getClass().getResource("AolRun.fxml"));
             SmartCC.fxmlval.add("AOLScreen.fxml");
             SmartCC.stageval.add((Stage) run.getScene().getWindow());
             Stage stage = new Stage();
             stage.initStyle(StageStyle.UTILITY);
             stage = (Stage) run.getScene().getWindow();
             Scene scene = new Scene(parent);
             stage.setScene(scene);
             scene.getStylesheets().add("/css/Style.css");
             stage.setTitle("AOL Output ");
             stage.setResizable(false);
             stage.show();
         } catch (IOException ex) {
             Logger.getLogger(AOLScreenController.class.getName()).log(Level.SEVERE, null, ex);
         }
     }
 });

If I place it inside the last task there is thread synchronization errors and I do not know about that.
I am very new to threads and javafx threads please help

like image 993
Rahul Singh Avatar asked Nov 17 '25 15:11

Rahul Singh


1 Answers

You could just add a onSucceded handler to the tasks. This handler is run on the application thread after successful completion of the tasks:

public void runEverything() {
    ...

    EventHandler<WorkerStateEvent> handler = new EventHandler<WorkerStateEvent>() {
         private int remainingTasks = tasks.size();

         @Override
         public void handle(WorkerStateEvent event) {
              if ((--remainingTasks) == 0) {
                  try {
                      Parent parent = FXMLLoader.load(getClass().getResource("AolRun.fxml"));
                      SmartCC.fxmlval.add("AOLScreen.fxml");
                      SmartCC.stageval.add((Stage) run.getScne().getWindow());
                      Stage stage = new Stage();
                      stage.initStyle(StageStyle.UTILITY);
                      stage = (Stage) run.getScene().getWindow();
                      Scene scene = new Scene(parent);
                      stage.setScene(scene);
                      scene.getStylesheets().add("/css/Style.css");
                      stage.setTitle("AOL Output ");
                      stage.setResizable(false);
                      stage.show();
                  } catch (IOException ex) {
                      Logger.getLogger(AOLScreenController.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
         }

    };

    ExecutorService threadPool = Executors.newFixedThreadPool(5);
    for (Task t : tasks) {
        t.setOnSuceeded(handler);
        threadPool.submit(t);
    }
    threadPool.shutdown();
}
like image 68
fabian Avatar answered Nov 20 '25 13:11

fabian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!