我有一個應用程序啓動一個計時器來在用戶操作上顯示一條消息。在JDK profiler中,似乎所有其他線程在被GC執行後被刪除(我猜),但創建的定時器沒有被刪除。那裏會發生什麼?Java - 計時器在執行後沒有被移除
我的定時器:
/**
* @param owner
* @param added
*/
public static void splashParentWithAnimation(AnchorPane owner, Parent added,double posX,double posY) {
// addParentWithAnimation(owner, added);
owner.getChildren().add(added);
AnchorPane.setLeftAnchor(added, posX);
AnchorPane.setTopAnchor(added, posY);
FadeTransition ft1 = new FadeTransition(Duration.millis(300), added);
ft1.setFromValue(0.0);
ft1.setToValue(1.0);
ft1.play();
Timer messagePrinter = new Timer();
messagePrinter.schedule(new TimerTask() {
@Override
public void run() {
Platform.runLater(() -> {
if (!owner.getChildren().contains(added))
return;
FadeTransition ft1 = new FadeTransition(Duration.millis(300), added);
ft1.setFromValue(1.0);
ft1.setToValue(0.0);
ft1.play();
ft1.setOnFinished((e) -> {
if (owner.getChildren().contains(added))
owner.getChildren().remove(added);
});
});
}
}, 1000);
}
難道是因爲我使用的是靜態方法還是應該摧毀它自己?
我不確定,但很高興知道,反正,我用javaFX的時間軸,而不是(至少對我來說)更好 –