如何在 JavaFx 中设置任务超时?
How can I set Task timeout in JavaFx?
我有一个class,其中一种方法是通过 USB 设备获取数据并将其添加到 ArrayList 中:
public class usbDevice {
public Task<Void> readData() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
//read data through usb device and add it into array;
return null;
}
};
}
}
我在按下控制器 class 内的按钮时调用此方法,它扩展了 USB_device class:
@FXML
void readUSB(ActionEvent event) {
try {
Parent root = FXMLLoader.load(getClass().getResource("../resources/loadingPage.fxml"));
Scene scene = startButton.getScene();
root.translateXProperty().set(scene.getWidth());
parent.getChildren().add(root);
Timeline timeline = new Timeline();
KeyValue keyValue = new KeyValue(root.translateXProperty(), 0 , Interpolator.EASE_IN);
KeyFrame keyFrame = new KeyFrame(Duration.millis(100), keyValue);
timeline.getKeyFrames().add(keyFrame);
timeline.setOnFinished(event1 -> {
parent.getChildren().remove(container);
Task<Void> readTask = readData();
Thread t = new Thread(readTask);
t.start();
});
timeline.play();
} catch (IOException e) {
e.printStackTrace();
}
}
但有时我会卡在这种方法中,如果 10 秒后我需要重新连接到我的 USB 设备,例如,我没有获取数据。我如何设置超时,在此线程中每 10 秒重新连接我的 usb 设备,直到我通过它获取数据?
提前致谢
您可以使用 CompletableFuture
:
public class USBDevice {
public Task<Void> readData() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//read data through usb device and add it into array;
});
try {
future.get(10, TimeUnit.SECONDS);
// get here if read was successful
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
} catch (ExecutionException ee) {
// exception was thrown by code reading usb device
} catch (TimeoutException te) {
// timeout occurred
}
return null;
}
};
}
}
我有一个class,其中一种方法是通过 USB 设备获取数据并将其添加到 ArrayList 中:
public class usbDevice {
public Task<Void> readData() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
//read data through usb device and add it into array;
return null;
}
};
}
}
我在按下控制器 class 内的按钮时调用此方法,它扩展了 USB_device class:
@FXML
void readUSB(ActionEvent event) {
try {
Parent root = FXMLLoader.load(getClass().getResource("../resources/loadingPage.fxml"));
Scene scene = startButton.getScene();
root.translateXProperty().set(scene.getWidth());
parent.getChildren().add(root);
Timeline timeline = new Timeline();
KeyValue keyValue = new KeyValue(root.translateXProperty(), 0 , Interpolator.EASE_IN);
KeyFrame keyFrame = new KeyFrame(Duration.millis(100), keyValue);
timeline.getKeyFrames().add(keyFrame);
timeline.setOnFinished(event1 -> {
parent.getChildren().remove(container);
Task<Void> readTask = readData();
Thread t = new Thread(readTask);
t.start();
});
timeline.play();
} catch (IOException e) {
e.printStackTrace();
}
}
但有时我会卡在这种方法中,如果 10 秒后我需要重新连接到我的 USB 设备,例如,我没有获取数据。我如何设置超时,在此线程中每 10 秒重新连接我的 usb 设备,直到我通过它获取数据? 提前致谢
您可以使用 CompletableFuture
:
public class USBDevice {
public Task<Void> readData() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//read data through usb device and add it into array;
});
try {
future.get(10, TimeUnit.SECONDS);
// get here if read was successful
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
} catch (ExecutionException ee) {
// exception was thrown by code reading usb device
} catch (TimeoutException te) {
// timeout occurred
}
return null;
}
};
}
}