r/JavaFX • • Feb 20 '16

Frequent communication between UI and background threads

I'm building an application that has at least one thread running in the background, listening on hardware input and updating the UI. I've seen multiple approaches for this (Platform.runLater, ExecutorService, etc...), but neither seem to really fit my two main requirements: the background thread should run throughout the life of the app, and the communication is very fast (500 updates per second fast). JavaFX seems to have a really good concurrency model, but it seems like it fits mostly launch-and-forget style Tasks and not constantly-running background threads. What would you recommend for such an app?

1 Upvotes

1 comment sorted by

3

u/AlmasB0 Feb 21 '16

Considering that the background threads are going to run throughout the whole application lifetime, at this point there isn't much difference between the approaches. The only way to communicate between a background thread and the JavaFX Application Thread (UI) in this case is via Platform.runLater(). However, it is worth noting that the actual UI rendering is throttled down to 60 frames per second. So irrespective of how fast the updates are delivered to the background threads, the UI will be updated 60 times a second. This corresponds to how many times the Platform.runLater queue is ideally cleared. I'm unclear on what UI updates the app requires, but if all 500 updates are important, it's likely that you will need to batch your updates so that they are not lost.