gpt4 book ai didi

java - 如何更改计时器/线程的延迟

转载 作者:行者123 更新时间:2023-12-02 02:14:31 25 4
gpt4 key购买 nike

所以我试图有一个有计时器或线程的方法,这样我就可以每N分钟自动保存一次。(N是分钟数)我从我的XML文件中得到N,它可以随时更改。到目前为止,这就是我想出的

private void autoSave(){
Timer autoSaveTimer = new Timer();

autoSaveTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("Auto saving :D");
VB.save();
}
}, 0, autoSaveTime());
}

自动保存时间();只是从 XMl 文件中读取分钟数并将其转换为毫秒并返回该值。目前如果我改变 N,它不会改变。

如果有不同的方法,我很乐意倾听。

最佳答案

考虑使用预定服务:

public class AutoSaveService implements ScheduledService<Void> {

@Override
protected Task<Void> createTask() {
// retrieve data from UI. This should be done here,
// as you should access the data on the FX Application Thread
final MyDataType data = getDataFromUI();
return new Task<Void>() {
@Override
protected Void call() throws Exception {
vb.save(data);
return null ;
}
};
}
}

您可以按如下方式使用它:

AutoSaveService autoSaveService = new AutoSaveService();
autoSaveService.setPeriod(Duration.seconds(5));
autoSaveService.start();

autoSaveService.setPeriod(...) 的调用将反射(reflect)在创建后续任务之前的时间。

你还可以做类似的事情

autoSaveService.setOnFailed(e -> {
Throwable whatWentWrong = autoSaveService.getException();
// log exception, warn user, etc...
});

这是一个 SSCCE(仅打印消息而不保存数据):

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class AutoSaveExample extends Application {

@Override
public void start(Stage primaryStage) {

Spinner<Integer> saveIntervalSpinner = new Spinner<>(1, 60, 1);

AutoSaveService autoSaveService = new AutoSaveService();
autoSaveService.periodProperty().bind(Bindings.createObjectBinding(
() -> Duration.seconds(saveIntervalSpinner.getValue()),
saveIntervalSpinner.valueProperty()));
autoSaveService.start();

VBox root = new VBox(5, new Label("Save Interval:"), saveIntervalSpinner);
root.setAlignment(Pos.CENTER_LEFT);
root.setPadding(new Insets(18));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}

public static class AutoSaveService extends ScheduledService<Void> {
@Override
protected Task<Void> createTask() {
// retrieve data from UI. This should be done here,
// as you should access the data on the FX Application Thread
// final MyDataType data = getDataFromUI();
return new Task<Void>() {
@Override
protected Void call() throws Exception {
// vb.save(data);
System.out.println("Save at "+System.currentTimeMillis());
return null ;
}
};
}
}


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

关于java - 如何更改计时器/线程的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49480644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com