ich ein Problem auftreten javafx in der Entwicklung, finde ich latch
hat keine Auswirkung in JavaFx zum Beispiel in dem folgenden Code:Wie Latch in javafx keine Wirkung hat?
public class JavafxLatchDemo1 extends Application {
@Override
public void start(Stage primaryStage) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
TextArea txtOut = new TextArea();
StackPane root = new StackPane();
root.getChildren().add(txtOut);
Scene scene = new Scene(root, 300, 250);
//invoke rpc function
Callable<Integer> fibCall = new fibCallable(latch, txtOut);
FutureTask<Integer> fibTask = new FutureTask<Integer>(fibCall);
Thread fibThread = new Thread(fibTask);
fibThread.start();
latch.await(); //阻塞等待计数为0
txtOut.appendText("\n Say 'Hello World'");
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
class fibCallable implements Callable<Integer>{
CountDownLatch latch;
TextArea txtInput;
fibCallable(CountDownLatch mylatch, TextArea txtIn){
latch = mylatch;
txtInput = txtIn;
}
@Override
public Integer call() throws Exception {
int temp1=1,temp2=0;
System.out.println("Client will pay money for eshop");
for(int i=0; i<10; i++){
temp1 = temp1 + temp2;
temp2 = temp1;
}
System.out.println("Client already decide to pay money for eshop");
Platform.runLater(()->{
txtInput.appendText("\nWhy, am I first?");
});
latch.countDown(); //计数减1
return (new Integer(temp1));
}
}
Da ich einen Riegel gesetzt JavaFx Hauptthread zu stoppen in latch.await();
, und wollen das aufrufbare Thread fibCallable
Ausgang der Inhalt zuerst: so erwarte ich:
Why, am I first?
Say 'Hello World'
aber die wirkliche Ausg ut ist das Gegenteil:
Say 'Hello World'
Why, am I first?
warum? und eine Lösung?
tnanks, Sie sind gut –
@EastJiang Markieren Sie die Antwort als richtig, wenn sie die Frage beantwortet –