Reputation: 83
I have a JavaFX application that runs multiple threads that do heavy computing. The problem is that after a while the UI completely freezes, but I only update the UI with Platform.runLater()
The way I start my main thread that spawns the rest of the threads:
mainThread = new MainThread(mc);
mainThread.start();
Here I give the thread the reference to the main Controller, which passes this reference to the rest of the threads so they can print stuff to a TextArea.
The main thread spawns only 2 sub-threads at once, but these two sub-threads use ExecutorService with configurable number of threads(100+):
executor = Executors.newFixedThreadPool((Integer.valueOf(mainController.getIndexController().getThreadsField().getText())));
for(int i = 0; i < newTasks.size(); i++){
Runnable slaveThread = new SlaveThread(dataLink, url);
executor.execute(slaveThread );
}
Now threads do lots of stuff like downloading files, but I guess that should not affect the UI. They occasionally read from the interface, but that shouldn't be the problem.
I thought that by using platform.runlater(), the UI can't be frozen.
Platform.runLater(() -> {
mainController.getIndexController().writeToConsole(result);
});
Since I don't have any other code that modifies the UI, this must be the problem. The program executes many-many Platform.runLater()-s, is it too much? Or how much is too much? There are other applications with smooth UI that update the interface way more frequently than mine and run just fine. What can be the problem? Thanks.
Upvotes: 2
Views: 1824
Reputation: 10650
The fact that you say that the UI completely freezes sounds to me more like a dead-lock condition than using too many Platform.runLater calls. There are various tools which can be used to detect dead-locks. Using too many Platform.runLater calls should normally just make the system slower because there is a queue of calls building up.
Upvotes: 0
Reputation: 209330
It's impossible to know for sure from your question, but it sounds as though you are just scheduling to many runnables to the FX Application Thread by calling Platform.runLater(...)
too many times. "How many is too many" is not answerable, as it depends on many factors, but generally you should not schedule them more frequently than they can be consumed.
Typically you can do this by only scheduling a new update to the UI when a previous update that was scheduled has been executed. See Throttling javafx gui updates for techniques for doing this.
Upvotes: 2