I create two standard threads (Thread class) which each encapsulate a JavaFX thread with the Platform.runLater(...) method.
Until then the two java threads are created correctly.
Then I start both threads using the start() method of each. In debugging mode, I can see that the two threads start separately.
The problem is that the two JavaFX threads seem not to be independent, since in my program the second JavaFX thread waits for the first to be completed to start.
I must be missing some information on FX threads because I have already created in standard java and have never had this kind of problem.
Thank you very much for your help and answers.
Each FX Thread works on a different Task.
The first Task calculates and the second Task displays in a Label every second the progress percentage.
public void init() {
computetask = new Task<Void>() {
@Override
public Void call() {
//----Future Use--------------------------------------
// The folowing commented method takes approximately
// 30 to 40 minutes and generates 10 million lines
// in the history table for a period of 20 years.
// For the moment and the time to resolve the
// non-concurrent thread problem the program only does
// basic counting.
//----------------------------------------
//spe.computeStoksEvolution(labelmessage);
//--------------------------------------------------
double total = 100.;
current = 0;
while(current <= total) {
percent = ((current/total) * 100.);
current += 1.;
System.out.println("Incremental loop: " + percent);
}
stockssynthesisui.labelmessage.setTextFill(Color.BLUE);
stockssynthesisui.labelmessage.setText("Frequencies Computing ended.");
stockssynthesisui.buttonfrequency.setDisable(false);
return null;
}
printpecentingtask = new Task<Void>() {
@Override
protected Void call() throws Exception {
int count = 0;
//while(percent <= 100.) {
for(count=1; count <= 5; count++) {
percenttext = String.format("%03.06f", percent);
stockssynthesisui.labelmessage.setText(percenttext);
System.out.println(percenttext + " %");
Thread.sleep(1000);
}
return null;
}
};
System.out.println("run percenting");
Thread t1 = new Thread( () -> {
Platform.runLater(printpecentingtask);
});
System.out.println("run computing");
Thread t2 = new Thread( () -> {
Platform.runLater(computetask);
});
t1.start();
t2.start();
System.out.println("Both thread are stated");
}
There is only one JavaFX application thread and you don’t create it, it is created by the JavaFX platform when it is started. You are just creating your own threads, they are not “JavaFX threads”.
Your issue is that you are running everything in your threads later on the single JavaFX thread. So your threads are pointless and everything is run sequentially on the JavaFX application thread, including the loops and sleep statements, which completely freeze your UI.
For more info on threads in JavaFX see: