Я пытаюсь создать приложение для викторины, используя JavaFX, поскольку я вызываю вопросы, используя
Q1.invoke();
Q2.invoke();
эти вопросы будут отображаться в потоке пользовательского интерфейса
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);
}
как только вопрос отображается, мне нужно подождать, пока я не получу ответ, если я не получил ответ, должен быть вызван следующий вопрос. Поэтому я ввел поток внутри метода отображения, чтобы дождаться тайм-аута
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();
является блокирующим вызовом, он также блокирует поток пользовательского интерфейса, как этого добиться, не блокируя поток пользовательского интерфейса.