mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-17 23:16:01 +00:00
add awaitInterruptibly methods
This commit is contained in:
@@ -14,7 +14,7 @@ nexusPublishing {
|
||||
|
||||
subprojects {
|
||||
group = 'dev.tommyjs'
|
||||
version = '2.3.1'
|
||||
version = '2.3.2'
|
||||
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'com.github.johnrengelman.shadow'
|
||||
|
||||
@@ -63,15 +63,17 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public T await(long timeoutMillis) throws TimeoutException {
|
||||
try {
|
||||
public T awaitInterruptibly() throws InterruptedException {
|
||||
this.latch.await();
|
||||
return joinCompletion(Objects.requireNonNull(getCompletion()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public T awaitInterruptibly(long timeoutMillis) throws TimeoutException, InterruptedException {
|
||||
boolean success = this.latch.await(timeoutMillis, TimeUnit.MILLISECONDS);
|
||||
if (!success) {
|
||||
throw new TimeoutException("Promise stopped waiting after " + timeoutMillis + "ms");
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return joinCompletion(Objects.requireNonNull(getCompletion()));
|
||||
}
|
||||
@@ -79,14 +81,20 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
@Override
|
||||
public T await() {
|
||||
try {
|
||||
this.latch.await();
|
||||
return awaitInterruptibly();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return joinCompletion(Objects.requireNonNull(getCompletion()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public T await(long timeoutMillis) throws TimeoutException {
|
||||
try {
|
||||
return awaitInterruptibly(timeoutMillis);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private T joinCompletion(PromiseCompletion<T> completion) {
|
||||
if (completion.isError())
|
||||
|
||||
@@ -124,6 +124,12 @@ public interface Promise<T> {
|
||||
|
||||
void completeExceptionally(@NotNull Throwable result);
|
||||
|
||||
@Blocking
|
||||
T awaitInterruptibly() throws InterruptedException;
|
||||
|
||||
@Blocking
|
||||
T awaitInterruptibly(long timeout) throws TimeoutException, InterruptedException;
|
||||
|
||||
@Blocking
|
||||
T await();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user