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

View File

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

View File

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