有人可以帮帮我吗?我有一个使用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中实现这样的东西?谢谢 :)
由于您扩展了类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);
}
}
在我看来,更好的方法是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;
}
}