我正在構(gòu)建一個(gè) java fx 應(yīng)用程序,并在其自己的線程上有一個(gè) while 循環(huán),但該循環(huán)不會等待 Dijkstra 方法返回?cái)?shù)組,我似乎通過使線程休眠 1000 毫秒來暫時(shí)解決了問題,但我不這樣做我真的不知道為什么這是有效的,但是有沒有更好的方法可以讓我等待該方法完成然后開始下一次迭代? public class Chasers { public volatile List<List<Integer>> Path; /////// volatile int ChaserX; volatile int ChaserY; volatile int PlayerX; volatile int PlayerY; boolean Continue=true;volatile int PathSize = 0;int dir = 5;volatile int i=0;Main MainPassed=new Main();DijkstraSolve PathFinder = new DijkstraSolve(); Task<Void> task = new Task<Void>() { @Override protected Void call() throws Exception { while (Continue) { i = 0; width = MainPassed.width; ChaserX = ((int) (PlayerIns.chaser.getCenterX() / width) * width); ChaserY = ((int) (PlayerIns.chaser.getCenterY() / width) * width); PlayerX = ((int) (PlayerIns.player.getCenterX() / width) * width); PlayerY = ((int) (PlayerIns.player.getCenterY() / width) * width); Path = PathFinder.Dijkstra(ChaserX, ChaserY, PlayerX, PlayerY, MainPassed);//wait for this to return?? PathSize = Path.size() - 1; } return null; } }; Thread th = new Thread(task); //th.setDaemon(true); th.start(); public void MoveChaser(Player PlayerIns){ Timeline timeline = new Timeline(new KeyFrame(Duration.millis(50), new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println(" current path "+Path); System.out.println("=============================="); } })); timeline.setCycleCount(Timeline.INDEFINITE); timeline.play();}}
再次循環(huán)之前等待循環(huán)內(nèi)的方法完成
婷婷同學(xué)_
2023-06-28 16:15:42