2

I am trying to do a bus simulation using multi threading in javafx. When I run the program, a window will appear and there will be a table showing the bus number, the position of the bus etc, and the data in the table will keep on updating. Here's my run() method in the Bus class.

public void run() {
    for(int i=start; i<=650; i+=1){
        posInt=i;
        //Update position
        pos.set(Integer.toString(posInt));
        if(i%10==0){
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    status.set("Returning to depot");
    for(int i=650; i>= end; i-=1){
        posInt=i;
        pos.set(Integer.toString(posInt));
        if(i%10==0){
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

And in the Javafx Main Class I used an Executor to execute the task. And I placed my code in the start method

@Override
public void start(Stage primaryStage) throws InterruptedException {
    this.primaryStage = primaryStage;
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
        rootLayout = (AnchorPane) loader.load();
        RootLayoutController controller = loader.getController();
        controller.setMainApp(this);
        controller.setLayout(rootLayout);
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();

        ScheduledExecutorService stp=Executors.newScheduledThreadPool(count);
        for(int i=0, a=0; i<count; i++, a+=100){
            stp.schedule((Runnable) BusData.get(i), a, TimeUnit.MILLISECONDS);
        }
        stp.shutdown();
        while(!stp.isTerminated()){
        }
        System.out.println("Finished all thread");


    } catch (IOException e) {
        e.printStackTrace();
    }

}

Here's the problem, I cant see the simulation. When I execute my program, The window will appear. Nothing will be shown. The window will load till the threads are finished and "Finished all thread" is printed in the console. Then only will the table appear in the window. And all I would see is the end result, no simulation. Please help.

oyc
  • 21
  • 2
  • 1
    Read my answer to http://stackoverflow.com/questions/30249493/using-threads-to-make-database-requests, which explains the basic rules for multithreading in JavaFX. You violate both rules in this code: you block the FX Application Thread with your busy-while loop, so it cannot render anything, and you update the UI on a background thread (so your `Runnable`s are probably all terminating with exceptions). – James_D Sep 08 '15 at 12:27
  • For the for-loop, I have to execute it on the javafx thread because it updates the UI. However the for-loop takes a very long time to run, so that when I execute it on a javafx thread, the window shows up but it hangs there. What can I do to solve this? – oyc Sep 10 '15 at 16:12
  • Do the things that take a long time on a background thread, and update the UI on the FX Application Thread. Surely what's causing your app to hang is the `while` loop, which basically waits on the FX Application Thread until everything is finished. – James_D Sep 10 '15 at 16:15

0 Answers0