소스 검색

await now supports caller-supplied sleepInterval and sleepCallback

tags/2.0.1
Jonathan Cobb 4 년 전
부모
커밋
2ee619625e
1개의 변경된 파일37개의 추가작업 그리고 4개의 파일을 삭제
  1. +37
    -4
      src/main/java/org/cobbzilla/util/daemon/Await.java

+ 37
- 4
src/main/java/org/cobbzilla/util/daemon/Await.java 파일 보기

@@ -9,8 +9,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.cobbzilla.util.daemon.ZillaRuntime.die;
import static org.cobbzilla.util.daemon.ZillaRuntime.now;
import static org.cobbzilla.util.daemon.ZillaRuntime.*;
import static org.cobbzilla.util.system.Sleep.sleep;

@Slf4j
@@ -18,6 +17,7 @@ public class Await {

public static final long DEFAULT_AWAIT_GET_SLEEP = 10;
public static final long DEFAULT_AWAIT_RETRY_SLEEP = 100;
public static final long DEFAULT_AWAIT_ALL_SLEEP = 200;

public static <E> E awaitFirst(Collection<Future<E>> futures, long timeout) throws TimeoutException {
return awaitFirst(futures, timeout, DEFAULT_AWAIT_RETRY_SLEEP);
@@ -136,10 +136,36 @@ public class Await {
}

public static <T> AwaitResult<T> awaitAll(Collection<Future<?>> futures, long timeout) {
return awaitAll(futures, timeout, ClockProvider.SYSTEM);
return awaitAll(futures, timeout, ClockProvider.SYSTEM, DEFAULT_AWAIT_ALL_SLEEP, null);
}

public static <T> AwaitResult<T> awaitAll(Collection<Future<?>> futures, long timeout, ClockProvider clock) {
return awaitAll(futures, timeout, clock, DEFAULT_AWAIT_ALL_SLEEP, null);
}

public static <T> AwaitResult<T> awaitAll(Collection<Future<?>> futures, long timeout, Runnable sleepCallback) {
return awaitAll(futures, timeout, ClockProvider.SYSTEM, DEFAULT_AWAIT_ALL_SLEEP, sleepCallback);
}

public static <T> AwaitResult<T> awaitAll(Collection<Future<?>> futures,
long timeout,
long sleepTime,
Runnable sleepCallback) {
return awaitAll(futures, timeout, ClockProvider.SYSTEM, sleepTime, sleepCallback);
}

public static <T> AwaitResult<T> awaitAll(Collection<Future<?>> futures,
long timeout,
ClockProvider clock,
Runnable sleepCallback) {
return awaitAll(futures, timeout, clock, DEFAULT_AWAIT_ALL_SLEEP, sleepCallback);
}

public static <T> AwaitResult<T> awaitAll(Collection<Future<?>> futures,
long timeout,
ClockProvider clock,
long sleepTime,
Runnable sleepCallback) {
long start = clock.now();
final AwaitResult<T> result = new AwaitResult<>();
final Collection<Future<?>> awaiting = new ArrayList<>(futures);
@@ -161,7 +187,14 @@ public class Await {
}
}
if (awaiting.isEmpty()) break;
sleep(200);
if (sleepCallback != null) {
try {
sleepCallback.run();
} catch (Exception e) {
log.warn("awaitAll: exception in sleepCallback: "+shortError(e));
}
}
sleep(sleepTime);
}

result.timeout(awaiting);


불러오는 중...
취소
저장