Browse Source

await now supports caller-supplied sleepInterval and sleepCallback

tags/2.0.1
Jonathan Cobb 4 years ago
parent
commit
2ee619625e
1 changed files with 37 additions and 4 deletions
  1. +37
    -4
      src/main/java/org/cobbzilla/util/daemon/Await.java

+ 37
- 4
src/main/java/org/cobbzilla/util/daemon/Await.java View File

@@ -9,8 +9,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; 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; import static org.cobbzilla.util.system.Sleep.sleep;


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


public static final long DEFAULT_AWAIT_GET_SLEEP = 10; public static final long DEFAULT_AWAIT_GET_SLEEP = 10;
public static final long DEFAULT_AWAIT_RETRY_SLEEP = 100; 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 { public static <E> E awaitFirst(Collection<Future<E>> futures, long timeout) throws TimeoutException {
return awaitFirst(futures, timeout, DEFAULT_AWAIT_RETRY_SLEEP); 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) { 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) { 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(); long start = clock.now();
final AwaitResult<T> result = new AwaitResult<>(); final AwaitResult<T> result = new AwaitResult<>();
final Collection<Future<?>> awaiting = new ArrayList<>(futures); final Collection<Future<?>> awaiting = new ArrayList<>(futures);
@@ -161,7 +187,14 @@ public class Await {
} }
} }
if (awaiting.isEmpty()) break; 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); result.timeout(awaiting);


Loading…
Cancel
Save