Background Info
Task javadoc includes numerous concurrency usage patterns for passing data between threads in JavaFX.
Task includes convenience data transfer methods such as updateMessage and can be used instead of your Runnable with the user defined status property.
When appropriate, consider using a collection structure designed for concurrency, such as a BlockingQueue. An additional advantage is that BlockingQueues can have size limits, which seems like something you want.
Some general advice
- Be very careful when using mutable observable items in multiple threads. It is easy to inadvertently trigger updates that result in race conditions, updates to the active scene graph off the application thread and other threading issues.
- As much as possible, use immutable data rather than mutable observable items.
- Leverage some of the higher level utilities from the JavaFX concurrency and java.util.concurrent libraries.
- Avoid explicit synchronization and notify statements as much as possible.
- Be careful placing synchronization or other potentially blocking statements in code that runs on the JavaFX Application Thread - as you may make your GUI unresponsive.
- Use the JavaFX concurrency utilities only when you need interaction with the JavaFX thread.
- Do a lot of your very complicated multi-threaded processing off of the JavaFX thread using standard Java concurrency utilities. Have a single co-ordinating JavaFX Task for coalescing and controlling UI feedback.
The above are just rules of thumb and don't need to be followed didactically.
Reasonably Complex Threading Samples
- A chart renderer that demonstrates some of the principles above to render 300 charts while still keeping the UI responsive to progress updates and user interaction.