1

I am trying to run a code with a GUI interface. But I need the GUI windows all to be closed before I proceed with my main method, (some of the information that I need is collected from the GUI windows and I cant run the rest of my code without that information). So, I decided to use CountDownLatch to create two threads (one of them being my main method and the other one the class with handles my GUI stuff). But when I run my code it gets stuck at the end of the GUI and it doesn't proceed with my code. does anyone know what is wrong with my code?

public static void main (String[] args) throws IOException,InterruptedException{
long start = System.currentTimeMillis();
        double longThreshold, shortThreshold, investment;
        System.out.println("hello");
        CountDownLatch startSignal = new CountDownLatch(1);
        CountDownLatch stopSignal = new CountDownLatch(1);
        ProgrammeSettings mySettings=new ProgrammeSettings(startSignal,stopSignal);
                new Thread(mySettings).start();  // mysettings object is the GUI stuff
        startSignal.countDown();
            stopSignal.await();
        longThreshold = mySettings.getlowerThreshold();
        shortThreshold = mySettings.getupperThreshold();
        investment =mySettings.getinvestment();
        System.out.println("hello");
}

also here is my code for CountDownLatch of the GUI stuff:

public class ProgrammeSettings implements Runnable {
private final CountDownLatch startSignal;
private final CountDownLatch stopSignal;

ProgrammeSettings(CountDownLatch startSignal, CountDownLatch doneSignal) {
      this.startSignal = startSignal;
      this.stopSignal = doneSignal;
   }
    public void graphicDesign(){
     // do some GUI stuff 
   }
@Override
public void run() {
    // TODO Auto-generated method stub
    try{
        startSignal.await();
        graphicDesign();
        stopSignal.countDown();
    }
    catch( InterruptedException e){

    }

}

}

Mehdi
  • 21
  • 1
  • 1
  • 4
  • You are saying that your code gets stuck in `graphicDesign()`, where you `do some GUI stuff`. It seems you are not showing the part of the code which is relevant to your problem. Do you run that GUI stuff in the EDT (Event Dispatch Thread)? – assylias Aug 08 '12 at 12:02
  • You should only be doing GUI stuff on the one GUI event thread. What are you trying to get these threads to do? – Peter Lawrey Aug 08 '12 at 12:03
  • @assylias no, it doesn't get stuck in graphicDesign(). it gets up to stopSignal.countDown(); and it exits this thread (well that's what I think from running the code in debug mode). but when it gets to stopSignal.await(); it doesn't proceed any further. also when I change CountDownLatch stopSignal = new CountDownLatch( 2 ); to CountDownLatch stopSignal = new CountDownLatch( 1 ); it finishes the code but obviously it doesnt stop until the thread is finished – Mehdi Aug 08 '12 at 12:07
  • @PeterLawrey I am trying to get some numbers with a gui interface and then use those number to do some calculations. I am only using one GUI event thread and thats graphicDesign() – Mehdi Aug 08 '12 at 12:10
  • And you are using `SwingUtils.invokeLater(....)` to update the GUI? – Peter Lawrey Aug 08 '12 at 12:12
  • sorry this is the first time that I am using multiple threads to run something and I am a beginner basically. I don't actually know why .invokeLater mathod should be used. – Mehdi Aug 08 '12 at 12:15

1 Answers1

2

The basic idea does work but you can't run the GUI code in any thread; it must run in Swing's UI thread.

So a better solution is to run your "wait for completion of the data entry" in a new thread and let the main thread handle the UI.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • oh, so I have to check if the data has been collected in say a new class and the do the .countdown for that method in the new class? – Mehdi Aug 08 '12 at 12:03
  • A simple solution would be to use a button which reads "Start" and which calls a method that checks and processes the data. – Aaron Digulla Aug 08 '12 at 12:33