I need to work with multiple threads in background in a JavaFX application. I have a screen with ten buttons and I need to \'bind\' each thread with the button that started
Quick Overview of JavaFX Concurrency is explained with the Oracle Tutorial).
Task and Service both implement the Worker interface, which offers many observable and FX thread safe properties. For example the runningProperty which you can bind to a Button
s disable property, but there are many more to be used directly or indirectly in your application.
The difference is, that the Task
is for one time use:
Task<V> task = new Task<>();
Thread taskThread = new Thread(task);
taskThread.start();
After that you can not restart or reuse this task, you have to create another one. Because this is somewhat tedious, the Service
was created. It allows to execute a Task
multiple times (internally a new Task is created every time).
And, as you may have seen, you can assign ThreadGroups and every other Thread
property by yourself when using the Task
. These properties can be set for the Service
too, but there you have to specify an Executor (where you can set the properties).