Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Waiting until a condition is true

Joined
Nov 5, 2014
Messages
505
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)
 
Conelander
Joined
Oct 30, 2014
Messages
3,573
I have my own code for it:
Code:
public class Waiting {

    public static boolean waitFor(final Condition c, final long timeout) {
        final long start = System.currentTimeMillis();
        System.out.println("Waiting on a condition for max " + timeout + " ms");
        while (System.currentTimeMillis() - start < timeout && !c.validate()) {
            Execution.delay(20, 30);
        }
        System.out.println("Waited, returning " + c.validate());
        return c.validate();
    }

    public interface Condition {
        public boolean validate();
    }

}
Feel free to use it.
 
Engineer
Joined
Jul 28, 2013
Messages
2,776
There's the entire Execution class which includes anything you could ever want regarding sleeping
 
Engineer
Joined
Jul 28, 2013
Messages
2,776
I have my own code for it:
Code:
public class Waiting {

    public static boolean waitFor(final Condition c, final long timeout) {
        final long start = System.currentTimeMillis();
        System.out.println("Waiting on a condition for max " + timeout + " ms");
        while (System.currentTimeMillis() - start < timeout && !c.validate()) {
            Execution.delay(20, 30);
        }
        System.out.println("Waited, returning " + c.validate());
        return c.validate();
    }

    public interface Condition {
        public boolean validate();
    }

}
Feel free to use it.
Why...
 
Author of MaxiBots
Joined
Dec 3, 2013
Messages
6,906
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)
 
Top