Wait x seconds or until a condition becomes true

Assuming you want what you asked for, as opposed to suggestions for redesigning your code, you should look at Awaitility.

For example, if you want to see if a file will be created within the next 10 seconds, you do something like:

await().atMost(10, SECONDS).until(() -> myFile.exists());

It’s mainly aimed at testing, but does the specific requested trick of waiting for an arbitrary condition, specified by the caller, without explicit synchronization or sleep calls. If you don’t want to use the library, just read the code to see the way it does things.

Which, in this case, comes down to a similar polling loop to the question, but with a Java 8 lambda passed in as an argument, instead of an inline condition.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)