如何在实际的不同线程中运行两个JavaFX UI [英] How to run two JavaFX UI in actual different threads

查看:142
本文介绍了如何在实际的不同线程中运行两个JavaFX UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,我们可以在支持javaFX应用程序线程的新线程中运行javafx UI更新。

We know, we can run javafx UI updates in a new thread with support of the javaFX Application thread.

  new Thread(new Runnable() {
            @Override
            public void run() {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                       //UI Stuff
                    }
                });
            }
        }).start();

当我们开始两个UI更新时,让我们考虑在两个不同的线程中处理两个阶段。

When we start two UI updates , lets think two stages processing in two different threads.

新线程(......
updateUI(stage1)

new Thread(...... updateUI(stage1)

新线程(.... ..
updateUI(stage2)

new Thread(...... updateUI(stage2)

updateUI方法将是这样的。

updateUI method will be something like this.

void updateUI(Stage stage) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                //UI Stuff
            }
        });
    }

问题是这两个阶段永远不会同时更新。我认为他们都在Platform.runLater中使用相同的javafx应用程序线程。

The problem is the two stages never update simultaneously. I think they both are using the same javafx Application thread in Platform.runLater.

我想同时更新这两个阶段。我该怎么做(我们知道可以在AWT中运行几个UI线程并使用JavaFX进行任何机会)?

I want to Update these two stages at the same time. How do I do that (We know can run several UI threads in AWT and swing, any chance with JavaFX)?

推荐答案

你无法控制JavaFX的线程系统,i从应用程序的角度来看,这是单线程。

You can't control the threading system for JavaFX, it's single threaded from an application point of view.

JavaFX应用程序线程由JavaFX系统创建。在 JavaFX中了解其工作原理架构概述。更多背景信息在以下答案中: Javafx:javafx.concurent之间的区别和Platform.runLater?多线程工具包:A失败的梦想?

The JavaFX application thread is created by the JavaFX system. Read about how this works in the JavaFX architecture overview. Further background information is in the answer to: Javafx: Difference between javafx.concurent and Platform.runLater? and Multithreaded toolkits: A failed dream?

对你问题中其他要点的评论

当你说我们可以在一个新线程中运行javafx UI更新时,这不是真的,因为你正在调用 Platform.runLater ,它将工作转移到JavaFX应用程序线程,因此您实际上并未运行更新在一个新的线程。您还要说明当您从多个线程调用Platform.runLater时问题是两个阶段永远不会同时更新 - 这正是Platform.runLater的文档所说的将要执行的操作在JavaFX应用程序线程上运行指定的Runnable未来一些未指定的时间 - 所以当你使用Platform.runLater时没有顺序保证。

When you say "we can run javafx UI updates in a new thread", that is not really true, because you are invoking Platform.runLater which shifts the work on to the JavaFX application thread, so you aren't really running updates in a new thread. You also state that when you call Platform.runLater from multiple threads "The problem is the two stages never update simultaneously" - which is exactly what the documentation for Platform.runLater says it will do "Run the specified Runnable on the JavaFX Application Thread at some unspecified time in the future" - so there are no sequencing guarantees when you utilize Platform.runLater.


我想更新这两个阶段同时

I want to Update these two stages at the same time

不是创建自己的线程,而是在JavaFX应用程序线程上顺序执行的代码中更新两个阶段的场景图(例如,在应用程序的start方法或事件处理程序中)。一旦您的应用程序在JavaFX应用程序线程上执行指令,所有应用程序指令将在JavaFX线程的下一个脉冲之前执行,该线程呈现修改后的场景图 - 因此从用户的角度来看,这两个阶段将同时更新为脉冲渲染它们。默认情况下,JavaFX将每秒发出60次渲染脉冲。如果你在JavaFX应用程序线程上做了很多工作,那么在下一个脉冲发生之前会有一个暂停(这就是为什么建议你在不到六十分之一秒内完成所有工作)。

Rather than creating your own threads, update the scene graphs for both stages in code executing sequentially on the JavaFX application thread (e.g. in the start method or an event handler for your application). Once your application is executing instructions on the JavaFX application thread, all application instructions will be executed before the next "pulse" of the JavaFX thread which renders the modified scene graph - so from a user point of view the two stages will update simultaneously as the pulse renders them. By default, JavaFX will issue rendering pulses sixty times a second. If you do a lot of work on the JavaFX application thread, then there will be a pause before the next pulse occurs (which is why it is advised that you get all of the work done in less than a sixtieth of a second).

如果你需要做很多工作(例如一些I / O或计算成本极其昂贵的任务,需要花费四分之一秒或更长时间),你需要使用前面引用的JavaFX并发工具来确保您不会冻结UI,然后只在所有并发任务完成后更新UI - 在答案中演示了各种技术:如何重置JavaFX2中任务之间的进度指示器?

If you have to do a lot of work (e.g. some I/O or incredibly computationally expensive task which will take a quarter of a second or more), you will need to make use of the JavaFX concurrency tools referenced earlier to ensure you aren't freezing your UI, then only update the UI once all concurrent tasks have completed - various techniques of which demonstrated in the answer to: How to reset progress indicator between tasks in JavaFX2?

这篇关于如何在实际的不同线程中运行两个JavaFX UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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