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.