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