add awaitInterruptibly methods

This commit is contained in:
WhatCats
2024-05-21 14:41:22 +02:00
parent 1269f6ae94
commit daa05d93a0
3 changed files with 26 additions and 12 deletions

View File

@@ -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'

View File

@@ -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())

View File

@@ -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();