我有一个可以长时间运行的任务,并且它正在阻塞。我必须等待此任务完成才能在 GUI 上显示结果。
我知道这个任务需要多长时间,所以我想用进度条通知用户还剩多少时间。
网上的例子都是这样的:
Task<Integer> task = new Task<Integer>() {
@Override
protected Integer call() throws Exception {
int iterations;
for (iterations = 0; iterations < 1000; iterations++) {
updateProgress(iterations, 1000);
// Now block the thread for a short time, but be sure
// to check the interrupted exception for cancellation!
try {
Thread.sleep(100);
} catch (InterruptedException interrupted) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
}
}
return iterations;
}
};
此代码在循环中阻塞,并使用 updateProgress
方法更新其进度。
我的案例如下:
Task<Integer> task = new Task<Integer>() {
@Override
protected Integer call() throws Exception {
try {
//This is my long running task.
Thread.sleep(10000);
} catch (InterruptedException interrupted) {
if (isCancelled()) {
updateMessage("Cancelled");
return null;
}
}
}
return iterations;
}
};
这不能在循环中工作,因此我无法使用 updateProgress
更新进度。
解决此问题的一种方法是创建另一个任务来运行我的阻塞任务,并计算进度,但这似乎不太优雅。
有更好的方法吗?
最佳答案
我发现很难看到任务阻塞的用例,但您以某种方式知道它需要多长时间(至少使用 animation API 不会更容易实现)。但假设你有一个,我会这样处理:
您的任务处于阻塞状态,因此必须在其他地方执行进度更新。基本上,您需要一个定期更新进度的后台线程。执行此操作的最佳时间是每次将帧渲染到屏幕时一次: AnimationTimer
class正是为此而设计的。因此,您希望任务在开始运行时启动动画计时器,更新 handle(...)
方法中的进度,并确保它在任务停止运行时停止。这看起来像:
public abstract class FixedDurationTask<V> extends Task<V> {
private final Duration anticipatedRuntime ;
private AnimationTimer timer ;
public FixedDurationTask(Duration anticipatedRuntime) {
this.anticipatedRuntime = anticipatedRuntime ;
}
public Duration getAnticipatedRuntime() {
return anticipatedRuntime ;
}
@Override
protected void running() {
timer = new AnimationTimer() {
long startTime = System.nanoTime() ;
long endTime = (long) (startTime + anticipatedRuntime.toMillis() * 1_000_000) ;
long duration = endTime - startTime ;
@Override
public void handle(long now) {
if (isRunning()) {
updateProgress(now - startTime, duration);
} else {
stop();
if (getState() == State.SUCCEEDED) {
updateProgress(duration, duration);
}
}
}
};
timer.start();
}
}
这是一个最小的测试(为了方便起见,包含上述类)。在文本字段中输入时间(以秒为单位),然后按 Enter。
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FixedDurationTaskTest extends Application {
@Override
public void start(Stage primaryStage) {
TextField durationField = new TextField();
durationField.setPromptText("Enter time in seconds");
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
double duration = Double.parseDouble(durationField.getText());
return new FixedDurationTask<Void>(Duration.seconds(duration)) {
@Override
public Void call() throws Exception {
Thread.sleep((long) getAnticipatedRuntime().toMillis());
return null ;
}
};
}
};
ProgressBar progress = new ProgressBar();
progress.progressProperty().bind(service.progressProperty());
durationField.disableProperty().bind(service.runningProperty());
durationField.setOnAction(e -> service.restart());
VBox root = new VBox(10, durationField, progress);
root.setPadding(new Insets(20));
root.setMinHeight(60);
root.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static abstract class FixedDurationTask<V> extends Task<V> {
private final Duration anticipatedRuntime ;
private AnimationTimer timer ;
public FixedDurationTask(Duration anticipatedRuntime) {
this.anticipatedRuntime = anticipatedRuntime ;
}
public Duration getAnticipatedRuntime() {
return anticipatedRuntime ;
}
@Override
protected void running() {
timer = new AnimationTimer() {
long startTime = System.nanoTime() ;
long endTime = (long) (startTime + anticipatedRuntime.toMillis() * 1_000_000) ;
long duration = endTime - startTime ;
@Override
public void handle(long now) {
if (isRunning()) {
updateProgress(now - startTime, duration);
} else {
stop();
if (getState() == State.SUCCEEDED) {
updateProgress(duration, duration);
}
}
}
};
timer.start();
}
}
public static void main(String[] args) {
launch(args);
}
}
关于java - 长时间运行和阻塞 javafx 任务进度条更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31311353/