6

What I am trying to achieve is to halt the thread and wait until doSomeProcess() is called before proceeding. But for some strange reason, the whole process got stuck at await and it never get into the Runnable.run.

Code snippet :

final CountDownLatch latch = new CountDownLatch(1); 
Platform.runLater(new Runnable() {
   @Override public void run() { 
     System.out.println("Doing some process");
     doSomeProcess();
     latch.countDown();
   }
});
System.out.println("Await");
latch.await();      
System.out.println("Done");

Console output :

Await
xar
  • 1,429
  • 2
  • 17
  • 29
  • Have you tried making an output before `doSomeProcess()`? I guess that function is not returning, that's all. – Fildor Jun 07 '13 at 07:35
  • I'd updated my code snippet. It can't even reach the run method. – xar Jun 07 '13 at 07:37
  • Is something blocking the GUI-Thread? Or are you executing the whole snipped on the GUI-Thread? – Fildor Jun 07 '13 at 07:40
  • I'm not sure. How am I suppose to know if it's being executed on the GUI-Thread? But the snippet above is the only piece of code I have. Nothing more. – xar Jun 07 '13 at 07:49
  • You can add an output of Thread.currentThread's name to see. If the Thread is waiting for a piece of code it is going to execute later... – Fildor Jun 07 '13 at 07:51
  • It's executed on JavaFX Application Thread. – xar Jun 07 '13 at 08:01
  • 1
    I guess that's the problem. If you are supposed to execute `doSomeProcess` asynchronously, then do it on a different Thread. I don't know JavaFX too much, perhaps there is some kind of "AsyncTask" class? – Fildor Jun 07 '13 at 08:02
  • possible duplicate of [Return result from javafx platform runlater](http://stackoverflow.com/questions/13796595/return-result-from-javafx-platform-runlater) – jewelsea Jun 07 '13 at 08:35

1 Answers1

5

The latch.countDown() statement will never be called since the JavaFX Thread is waiting for it to be called; when the JavaFX thread get released from the latch.wait() your runnable.run() method will be called.

I hope this code make the thing 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