The latch.countDown () statement will never be called since JavaFX Thread is waiting for it to be called; when the JavaFX thread is freed from the latch.wait () method, you will call your runnable.run () method.
Hope this code makes it clearer
final CountDownLatch latch = new CountDownLatch(1); // asynchronous thread doing the process new Thread(new Runnable() { @Override public void run() { System.out.println("Doing some process"); doSomeProcess(); // I tested with a 5 seconds sleep latch.countDown(); } }).start(); // asynchronous thread waiting for the process to finish new Thread(new Runnable() { @Override public void run() { System.out.println("Await"); try { latch.await(); } catch (InterruptedException ex) { Logger.getLogger(Motores.class.getName()).log(Level.SEVERE, null, ex); } // queuing the done notification into the javafx thread Platform.runLater(new Runnable() { @Override public void run() { System.out.println("Done"); } }); } }).start();
Console output:
Doing some process Await Done
source share