Waiting until a condition is true

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

  1. Ozzy

    Joined:
    Nov 5, 2014
    Messages:
    504
    Likes Received:
    159
    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. Defeat3d

    Defeat3d Prime Celestial

    Joined:
    Oct 30, 2014
    Messages:
    1,322
    Likes Received:
    559
    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. Ozzy

    Joined:
    Nov 5, 2014
    Messages:
    504
    Likes Received:
    159
    Great job that's just what I was looking for, thanks for sharing that.
     
  4. Cloud

    Cloud The Developer

    Joined:
    Jul 28, 2013
    Messages:
    2,329
    Likes Received:
    758
    There's the entire Execution class which includes anything you could ever want regarding sleeping
     
  5. Cloud

    Cloud The Developer

    Joined:
    Jul 28, 2013
    Messages:
    2,329
    Likes Received:
    758
    Why...
     
  6. Defeat3d

    Defeat3d Prime Celestial

    Joined:
    Oct 30, 2014
    Messages:
    1,322
    Likes Received:
    559
    I never realized it was in Execution, lmao.

    I guess I'll switch to the built in version asap, then.
     
  7. Aidden

    Aidden The better executive ;)

    Joined:
    Dec 3, 2013
    Messages:
    3,291
    Likes Received:
    665
    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...