allow the sleep time to be specified to allow finer grain return from a wait

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1363314 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2012-07-19 12:39:16 +00:00
parent eebe1358d2
commit f135b7ab76
1 changed files with 10 additions and 2 deletions

View File

@ -18,9 +18,12 @@
package org.apache.activemq.util; package org.apache.activemq.util;
import java.util.concurrent.TimeUnit;
public class Wait { public class Wait {
public static final long MAX_WAIT_MILLIS = 30*1000; public static final long MAX_WAIT_MILLIS = 30*1000;
public static final int SLEEP_MILLIS = 1000;
public interface Condition { public interface Condition {
boolean isSatisified() throws Exception; boolean isSatisified() throws Exception;
@ -31,10 +34,15 @@ public class Wait {
} }
public static boolean waitFor(final Condition condition, final long duration) throws Exception { public static boolean waitFor(final Condition condition, final long duration) throws Exception {
return waitFor(condition, MAX_WAIT_MILLIS, SLEEP_MILLIS);
}
public static boolean waitFor(final Condition condition, final long duration, final int sleepMillis) throws Exception {
final long expiry = System.currentTimeMillis() + duration; final long expiry = System.currentTimeMillis() + duration;
boolean conditionSatisified = condition.isSatisified(); boolean conditionSatisified = condition.isSatisified();
while (!conditionSatisified && System.currentTimeMillis() < expiry) { while (!conditionSatisified && System.currentTimeMillis() < expiry) {
Thread.sleep(1000); TimeUnit.MILLISECONDS.sleep(sleepMillis);
conditionSatisified = condition.isSatisified(); conditionSatisified = condition.isSatisified();
} }
return conditionSatisified; return conditionSatisified;