JavaFX多个任务

时间:2018-11-18 01:58:19

标签: java javafx concurrency parallel-processing task

有人可以帮我吗? 我有一个使用JavaFX的程序。 现在,按下按钮后,我要进行n计算。 这些计算应使用线程(并行)完成,并且不应使主JavaFX应用挂起/冻结。我想做的是在计算运行时显示加载状态,如果所有任务都已完成计算,我想继续执行程序(删除加载状态并显示结果)。

在阅读了有关“ JavaFX中的并发性”的一些知识之后,我想到了:

for (int i = 0; i < n; ++i) {
    CalcTask task = new CalcTask(i);

    task.setOnSucceeded(e -> {
        // process is just a static method in which I count 
        // how many results (i) I already received 
        // (where n is the required amount). If i == n,
        // I know I am done with all the tasks
        process(task.getValue());
    });

    new Thread(task).start();
}

还有CalcTask类:

public class CalcTask extends Task<Integer> {
    protected int id;

    public CalcTask (int id) {
        this.id = id;
    }

    @Override
    public Integer call() {
        return CALCULATION_RESULT;
    }
}

现在我的问题是:在我看来,这有点“笨拙”。有什么更好的方法可以在JavaFX中实现类似的东西?谢谢:)

2 个答案:

答案 0 :(得分:3)

自从扩展Task类以来,您还可以覆盖方法succeeded()并删除主线程上task.setOnSucceeded()的调用:

for (int i = 0; i < n; ++i) {
  CalcTask task = new CalcTask(i);
  new Thread(task).start();
}

public class CalcTask extends Task<Integer> {
    protected int id;

    public CalcTask (int id) {
        this.id = id;
    }

    public void succeeded() {
       process(this.getValue());
    }

    @Override
    public Integer call() {
        return CALCULATION_RESULT;
    }
}

或者甚至使用普通的Runnable而不是Task

public class CalcTask implements Runnable {
    protected int id;

    public CalcTask (int id) {
        this.id = id;
    }

    @Override
    public void run() {
       CALCULATION_RESULT = calculate();
       process(CALCULATION_RESULT);
    }
}

答案 1 :(得分:0)

我认为更好的方法是RxJavaFx
此示例:https://github.com/pkrysztofiak/rxjavafx-demo
RxJavaFx教程https://github.com/ReactiveX/RxJavaFX

package com.github.pkrysztofiak.rxjavafx.rxjavafxdemo.concurrency;

import java.util.Random;

import io.reactivex.Observable;
import io.reactivex.rxjavafx.observables.JavaFxObservable;
import io.reactivex.rxjavafx.schedulers.JavaFxScheduler;
import io.reactivex.schedulers.Schedulers;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ParallelTasksApp extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Button button = new Button("Start");
        Label label = new Label();
        HBox hBox = new HBox(button, label);
        stage.setScene(new Scene(hBox));
        stage.show();

        JavaFxObservable.actionEventsOf(button)
        .flatMap(actionEvent -> Observable.range(1, 4))
        .flatMap(i -> Observable.just(i)
                .subscribeOn(Schedulers.newThread())
                .map(this::runLongProcess))
        .observeOn(JavaFxScheduler.platform())
        .scan(0, (aggregator, next) -> ++aggregator)
        .map(String::valueOf)
        .subscribe(label::setText);
    }

    private int runLongProcess(int i) {
        try {
            Thread.sleep(new Random().nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " i=" + i);
        return i;
    }
}