Multithreading in JavaFX

前端 未结 1 1846
星月不相逢
星月不相逢 2020-12-06 22:52

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

相关标签:
1条回答
  • 2020-12-06 22:54

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

    0 讨论(0)
提交回复
热议问题