我试图理解,是否Platform.runLater()方法仅调用SAME JavaFxApplicationThread,如start()方法或同名的另一个线程?
在下面的代码中:
1)首先,使用JavaFxApplicationThread调用了start()方法;
2)然后,调用startFilling()方法,从此处开始新线程;
3),最后,使用openMessage()方法打开了新的Stage,其中使用了Platform.runLater()方法。
请查看代码:
public class FillingTimeLine extends Application {
private LongProperty lp = new SimpleLongProperty(0);
private Timeline timeline = new Timeline();
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
System.out.println("\nSTARTING THREAD: " + Thread.currentThread().getName());
StackPane spane = new StackPane();
ProgressBar pb = new ProgressBar(0);
pb.setMinSize(160, 21.5);
pb.setMaxSize(160, 21.5);
pb.setPrefSize(160, 21.5);
pb.progressProperty().bind(lp.divide(10000 * 1.0));
pb.setStyle("-fx-base:darkgray;-fx-accent:red;");
spane.getChildren().add(pb);
Scene scene = new Scene(spane, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
startFilling();
openNewStage();
}
public void startFilling() throws InterruptedException {
new Thread(() -> {
System.out.println("\nSTARTING THREAD: " + Thread.currentThread().getName());
timeline = new Timeline(new KeyFrame(Duration.seconds(0), new KeyValue(lp, 0)),
new KeyFrame(Duration.seconds(20), new KeyValue(lp, 20000)));
if (Thread.currentThread().isInterrupted()) {
System.out.println("\n THR WAS INTERRUPTED!");
return;
}
timeline.play();
try {
Thread.sleep(20000);
} catch (InterruptedException ex) {
System.out.println("\n THR WAS INTERRUPTED!");
return;
}
}).start();
}
public void stopFilling() {
new Thread(() -> {
System.out.println("\nSTOPPING THREAD: " + Thread.currentThread().getName());
timeline.stop();
}).start();
}
public void openNewStage() {
Platform.runLater(() -> {
System.out.println("\nOPENNING THREAD" + Thread.currentThread().getName());
Stage qst = new Stage();
StackPane sp = new StackPane();
Button btn = new Button("STOP");
btn.setMaxHeight(25);
btn.setMinHeight(25);
btn.setPrefHeight(25);
btn.setMaxWidth(80);
btn.setMinWidth(80);
btn.setPrefWidth(80);
btn.setAlignment(Pos.CENTER);
btn.setOnAction(e -> {
try {
qst.close();
stopFilling();
} catch (Exception e1) {
return;
}
});
sp.getChildren().add(btn);
Scene scene = new Scene(sp, 200, 120);
qst.setX(50);
qst.setY(50);
qst.setScene(scene);
qst.setResizable(false);
qst.show();
});
}
public static void main(String[] args) {
launch(args);
}
}
我可以使用Platform.runLater()启用并行执行。而且我期望带有STOP按钮的新舞台将由另一个线程打开,因为JavaFxApplicationThread仍在忙于primaryStage。
但是CONSOLE PUTPUT显示,也使用JavaFxApplicationThread-作为primaryStage打开了新舞台:
开始阅读:JavaFX应用程序线程
启动线程:线程3
打开线程:JavaFX应用程序线程
停止线程:线程4
如果primaryStage仍在显示但未关闭,那么一个线程又如何打开新的Stage?
还是OPENING THREAD-另一个具有相同名称的并行线程?
提前谢谢