1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Waiting until a condition is true

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

  1. Ozzy

    Joined:
    Nov 5, 2014
    Messages:
    505
    Likes Received:
    162
    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 Primate

    Joined:
    Oct 30, 2014
    Messages:
    3,072
    Likes Received:
    1,894
    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:
    505
    Likes Received:
    162
    Great job that's just what I was looking for, thanks for sharing that.
     
  4. Cloud

    Cloud Engineer

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

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    Why...
     
  6. Defeat3d

    Defeat3d Primate

    Joined:
    Oct 30, 2014
    Messages:
    3,072
    Likes Received:
    1,894
    I never realized it was in Execution, lmao.

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

    Aidden Author of MaxiBots

    Joined:
    Dec 3, 2013
    Messages:
    6,600
    Likes Received:
    990
    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...