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

查看:180
本文介绍了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快速概述使用解释并发性Oracle教程)。

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

任务和服务都实现 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.

不同的是, 任务一次性使用:

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

并且,as您可能已经看到,在使用任务时,您可以自己分配ThreadGroups和其他所有 Thread 属性。这些属性也可以为服务设置,但是你必须指定一个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天全站免登陆