JavaFX 中的多线程 [英] Multithreading in JavaFX

查看:23
本文介绍了JavaFX 中的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 JavaFX 应用程序的后台处理多个线程.我有一个带有十个按钮的屏幕,我需要用启动线程的按钮绑定"每个线程.如果用户按下启动线程的按钮(在主屏幕 MainController.java 中),我需要恢复它以显示包含的信息以在详细信息屏幕(第二个屏幕,DetailController.java)的控件上显示它).

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?

最好的问候,

推荐答案

JavaFX Concurrency 的快速概览通过 Oracle 教程).

Quick Overview of JavaFX Concurrency is explained with the Oracle Tutorial).

Task 和 Service 都实现了 Worker 接口,它提供了许多可观察的和 FX 线程安全的属性.例如 runningProperty 你可以绑定到 Button 的禁用属性,但还有更多可以直接使用或 间接在您的应用程序中.

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.

区别在于,Task是一次性使用的:

The difference is, that the Task is for one time use:

Task<V> task = new Task<>();
Thread taskThread = new Thread(task);
taskThread.start();

此后您将无法重新启动或重复使用此任务,您必须创建另一个任务.因为这有点乏味,所以创建了 Service.它允许多次执行Task(每次在内部创建一个新的Task).

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

而且,正如您可能已经看到的,在使用 Task 时,您可以自己分配 ThreadGroups 和所有其他 Thread 属性.也可以为 Service 设置这些属性,但您必须在其中指定一个 Executor(您可以在其中设置属性).

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

这篇关于JavaFX 中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆