I'm new with JavaFX and I've a little problem with a thread: I can execute it twice and I can't find why.
Here is a sum-upt of my code:
Task<Void> task = new Task<Void>() {
@Override public Void call() throws ImageLoadedException, HomographyException, IOException {
try{
System.out.println("GO !");
return null;
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void succeeded() {
super.succeeded();
System.out.println("SUCCEEDED");
}
};
@FXML protected void launch(ActionEvent event){
new Thread(task).start();
}
When I click a first time the button who start my thread, my task run without any problem (my console display "GO !" and "SUCCEEDED").
But if I click a second time, nothing append. Am I doing something wrong ? Can't we use a thread more than once ?
From the Thread.start() documentation : No
From the Concurrency in JavaFX tutorial :
So, you have to consider the Service class rather than Task.
Edit: this should work for you:
Service service = new Service<>(task);Do it with a wraper class
With a button can fire new tasks