wait x seconds or until a condition becomes true
问题 How to wait x seconds or until a condition becomes true? The condition should be tested periodically while waiting. Currently I'm using this code, but there should be a short function. for (int i = 10; i > 0 && !condition(); i--) { Thread.sleep(1000); } 回答1: 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()