gpio
gpio

Reputation: 23

Multithreading in JavaFX

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 the thread. If the user pressed a button that had started a thread (in the main screen, MainController.java), I need to recover it to display the information that contains to display it on the controls of Details Screen ( a second screen, DetailController.java).

What Class do you recommend for this? Service?

https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/package-summary.html

It is possible to name the threads with any of these classes?

Best regards,

Upvotes: 2

Views: 1159

Answers (1)

eckig
eckig

Reputation: 11134

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 Buttons 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).

Upvotes: 1

Related Questions