4

How can you create a function or component etc that will stop all running code until a condition is met?

For example the same way as a JOptionPane will do, if I have this for example:

JOptionPane.showInputDialog(null, "Hello", "Title", 1);

Within a function etc and then print to the console afterwards it it will not print until I close the JOptionPane.

I am guessing this component has some sort of thread setup built in to do this but how could I duplicate that with my own functions?

So say for example I wanted to make JFrames delay everything until it was closed so it acts like a JOptionPane.

Or for example have a function that had multiple inputs which got updated and inside it did some maths with those and if it was a certain value returned a boolean, but then everything else but those was paused until the true boolean was returned.

I am guessing the solution is some sort of thread setup but I am quite new to Java and when I have coded in the past I have not really used threads so I cannot create a good stop-start/pause-run style function system yet.

Does anyone have any suggestions how to achieve this or better yet code examples showing this type of thing working?

Bart
  • 19,692
  • 7
  • 68
  • 77
zeddex
  • 1,260
  • 5
  • 21
  • 38

2 Answers2

8

You create a monitor (which is just a simple Object)

public static final Object monitor = new Object();
public static boolean monitorState = false;

Now you create a wait method

public static void waitForThread() {
  monitorState = true;
  while (monitorState) {
    synchronized (monitor) {
      try {
        monitor.wait(); // wait until notified
      } catch (Exception e) {}
    }
  }
}

and a method to unlock your waiters.

public static void unlockWaiter() {
  synchronized (monitor) {
    monitorState = false;
    monitor.notifyAll(); // unlock again
  }
}

So when you want to do something fancy, you can do it like this:

new Thread(new Runnable() {
  @Override
  public void run() {
    // do your fancy calculations
    unlockWaiter();
  }
}).start();

// do some stuff    
waitForThread();    
// do some stuff that is dependent on the results of the thread

Of course, there are many possibilities, this is only one version of how to do it.

brimborium
  • 9,362
  • 9
  • 48
  • 76
  • 2
    `wait()` should always be called in a loop, as stated by the Javadoc. – lhballoti Aug 23 '12 at 15:26
  • This looks like the type of thing i need however i cannot get your code working for some reason. Could you possibly update it to show a very basic condition and result? – zeddex Aug 23 '12 at 15:44
  • Actually it is working i just needed to wrap the unlockWaiter() in the condition. The working test i did was // Do your fancy calculations int v = 0; int go = 1; do { // Old Position v = v + 1; System.out.println("Looping " + v); // If updating if(v==1200) { go = 0; unlockWaiter(); } } while(go == 1); – zeddex Aug 23 '12 at 16:06
  • 1
    @lhballoti Thanks for the suggestion, I updated the answer accordingly. – brimborium Aug 23 '12 at 21:00
  • 1
    @zeddex Thanks for accepting. Please consider my edit (by suggestion of lhballoti). It is actually recommended to put the `wait()` into a loop. – brimborium Aug 23 '12 at 21:03
1

Have you tried making the thread sleep?

as simple as Thread.sleep(timeInMilliseconds)

check here

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Nicolas Brown
  • 1,546
  • 1
  • 10
  • 17
  • 1
    Yes, it is good if you know the time to wait or are delaying loops for a certain time however in this case it was not what i needed. Thanks for the reply though. – zeddex Aug 23 '12 at 16:09