Waiting until a condition is true

Discussion in 'Programming & Hardware' started by Ozzy, Nov 5, 2014.

  1. Is there an equivalent to the following available here? I've had a look but can't find anything, thanks in advance :)
    wait(Callable<Boolean> cond) - Blocks until the specified condition is satisfied (returns true)
     
  2. I have my own code for it:
    Code (Text):
    1. public class Waiting {
    2.  
    3.     public static boolean waitFor(final Condition c, final long timeout) {
    4.         final long start = System.currentTimeMillis();
    5.         System.out.println("Waiting on a condition for max " + timeout + " ms");
    6.         while (System.currentTimeMillis() - start < timeout && !c.validate()) {
    7.             Execution.delay(20, 30);
    8.         }
    9.         System.out.println("Waited, returning " + c.validate());
    10.         return c.validate();
    11.     }
    12.  
    13.     public interface Condition {
    14.         public boolean validate();
    15.     }
    16.  
    17. }
    Feel free to use it.
     
    Ozzy likes this.
  3. Great job that's just what I was looking for, thanks for sharing that.
     
  4. There's the entire Execution class which includes anything you could ever want regarding sleeping
     
  5. Why...
     
  6. I never realized it was in Execution, lmao.

    I guess I'll switch to the built in version asap, then.
     
  7. To be more specific, Execution.delayUntil(Callable, frequency, timeout) or Execution.delayUntil(Callable, timeout)

    In one of the coming releases, the formers parameters will be changed to (Callable, mintimeout, maxtimeout)
     

Share This Page

Loading...