JavaFX 中的 Platform.runLater 和 Task [英] Platform.runLater and Task in JavaFX

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

问题描述

我一直在对此进行一些研究,但至少可以说我仍然很困惑.

谁能给我一个具体的例子,说明何时使用 Task 以及何时使用 Platform.runLater(Runnable);?究竟有什么区别?何时使用这些是否有黄金法则?

如果我错了也请纠正我,但这两个对象"不是在 GUI 的主线程中创建另一个线程的一种方式(用于更新 GUI)?

解决方案

使用 Platform.runLater(...) 进行快速简单的操作,使用 Task 进行复杂和大操作.

示例:为什么我们不能使用 Platform.runLater(...) 进行长计算(取自以下参考).

问题:后台线程仅从 0 到 100 万计数并更新 UI 中的进度条.

使用 Platform.runLater(...) 编码:

final ProgressBar bar = new ProgressBar();新线程(新运行(){@Override public void run() {for (int i = 1; i <= 1000000; i++) {最终 int 计数器 = i;Platform.runLater(new Runnable() {@Override public void run() {bar.setProgress(计数器/1000000.0);}});}}).开始();

<块引用>

这是一大堆可怕的代码,是对自然的犯罪(并且一般编程).首先,你会失去脑细胞,只是看在这个 Runnables 的双重嵌套中.其次,它将淹没带有少量 Runnable 的事件队列——实际上有一百万个.显然,我们需要一些 API 来更容易地编写背景工作人员然后与 UI 进行通信.

使用任务编码:

Task task = new Task() {@Override public void call() {静态最终整数最大值 = 1000000;for (int i = 1; i <= max; i++) {updateProgress(i, max);}返回空;}};ProgressBar bar = new ProgressBar();bar.progressProperty().bind(task.progressProperty());新线程(任务).开始();

<块引用>

它没有之前代码中出现的任何缺陷

参考:JavaFX 2.0 中的工作线程

I have been doing some research on this but I am still VERY confused to say the least.

Can anyone give me a concrete example of when to use Task and when to use Platform.runLater(Runnable);? What exactly is the difference? Is there a golden rule to when to use any of these?

Also correct me if I'm wrong but aren't these two "Objects" a way of creating another thread inside the main thread in a GUI (used for updating the GUI)?

解决方案

Use Platform.runLater(...) for quick and simple operations and Task for complex and big operations .

Example: Why Can't we use Platform.runLater(...) for long calculations (Taken from below reference).

Problem: Background thread which just counts from 0 to 1 million and update progress bar in UI.

Code using Platform.runLater(...):

final ProgressBar bar = new ProgressBar();
new Thread(new Runnable() {
    @Override public void run() {
    for (int i = 1; i <= 1000000; i++) {
        final int counter = i;
        Platform.runLater(new Runnable() {
            @Override public void run() {
                bar.setProgress(counter / 1000000.0);
            }
        });
    }
}).start();

This is a hideous hunk of code, a crime against nature (and programming in general). First, you’ll lose brain cells just looking at this double nesting of Runnables. Second, it is going to swamp the event queue with little Runnables — a million of them in fact. Clearly, we needed some API to make it easier to write background workers which then communicate back with the UI.

Code using Task :

Task task = new Task<Void>() {
    @Override public Void call() {
        static final int max = 1000000;
        for (int i = 1; i <= max; i++) {
            updateProgress(i, max);
        }
        return null;
    }
};

ProgressBar bar = new ProgressBar();
bar.progressProperty().bind(task.progressProperty());
new Thread(task).start();

it suffers from none of the flaws exhibited in the previous code

Reference : Worker Threading in JavaFX 2.0

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

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