[英]wait for a task to be completed without blocking the UI in javafx
我正在嘗試使用JavaFX創建測驗應用程序,因為我正在使用調用問題
Q1.invoke();
Q2.invoke();
這些問題將顯示在UI線程上
public void display(McqQuestion mcqQuestion) {
resourceAsStream = getClass().getResourceAsStream("/mcqview.fxml");
fxmlLoader = new FXMLLoader();
if (executorService==null) executorService =Executors.newSingleThreadExecutor();
Parent root = null;
try {
root = fxmlLoader.load(resourceAsStream);
Mcqview controller = fxmlLoader.getController();
controller.setAnswer1(mcqQuestion.getAnswers().get(0));
//controller class has setters to accept question properties.
controller.multipleChoiceQuestionType = this;
this.view.getBorderPane().setCenter(root);
}
顯示問題后,我需要等待直到得到答案為止,如果沒有得到答案,則應該調用下一個問題。因此,我在display方法內引入了一個線程來等待超時
submit = executorService.submit(() -> {
try {
TimeUnit.SECONDS.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
try {
submit.get(20,TimeUnit.SECONDS);
System.out.println("waiting finished");
} catch (InterruptedException e) {
e.printStackTrace();
}
從future.get();
是一個阻塞調用,它也阻塞了UI線程,如何在不阻塞UI線程的情況下實現這一目標。
為此,請勿使用單獨的線程。 這只會使事情變得更困難。 JavaFX提供了無需等待並發問題的等待方式。
在這種情況下,可以從帶有onFinished
處理函數的PauseTransition
中進行onFinished
。 處理來自事件處理程序的用戶輸入答案。
private static class Question {
private final String questionText;
private final String answers[];
private final int correctAnswerIndex;
public Question(String questionText, String[] answers, int correctAnswerIndex) {
if (answers.length != 3) {
// for simplicity's sake allow only exactly 3 answers
throw new IllegalArgumentException();
}
this.questionText = questionText;
this.answers = answers;
this.correctAnswerIndex = correctAnswerIndex;
}
}
private VBox questionPane;
private Label questionText;
private Button[] answerButtons;
private PauseTransition pauseTransition;
private Question currentQuestion;
private void answer(int index) {
pauseTransition.stop(); // no longer wait for timeout
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText((index == currentQuestion.correctAnswerIndex)
? "correct answer"
: "incorrect answer");
// show result and exit
alert.showAndWait();
Platform.exit();
}
private void ask(Question question) {
questionText.setText(question.questionText);
for (int i = 0; i < 3; i++) {
answerButtons[i].setText(question.answers[i]);
}
currentQuestion = question;
pauseTransition.playFromStart(); // start timeout timer
}
private void timeout() {
pauseTransition.stop();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText("your time ran out");
// cannot use showAndWait form animation directly
Platform.runLater(() -> {
// show result and exit
alert.showAndWait();
Platform.exit();
});
}
@Override
public void start(Stage stage) {
pauseTransition = new PauseTransition(Duration.seconds(10));
pauseTransition.setOnFinished(evt -> timeout());
questionText = new Label();
questionText.setWrapText(true);
questionPane = new VBox(questionText);
questionPane.setPrefSize(400, 400);
answerButtons = new Button[3];
for (int i = 0; i < 3; i++) {
final int answerIndex = i;
Button button = new Button();
button.setOnAction(evt -> answer(answerIndex));
answerButtons[i] = button;
questionPane.getChildren().add(button);
}
Scene scene = new Scene(questionPane);
stage.setScene(scene);
stage.show();
Question question = new Question(
"What is the answer to the ultimate question of life, the universe, and everything?",
new String[]{"Mew", "42", "Peanut butter"},
1
);
ask(question);
}
您可以輕松地以不同的方式實現超時或回答問題的結果,例如,提出下一個問題或在完成最后一個問題時顯示結果。
對於UI更改,您應該使用
Platform.runLater(() -> {
});
對於線程,您應該使用:
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
return null;
}
};
並將任務對象傳遞給
executorService.submit(task)
希望對您有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.