mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-18 07:16:45 +00:00
allow joining without a timeout
This commit is contained in:
@@ -14,7 +14,7 @@ nexusPublishing {
|
|||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
group = 'dev.tommyjs'
|
group = 'dev.tommyjs'
|
||||||
version = '2.3.0'
|
version = '2.3.1'
|
||||||
|
|
||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
apply plugin: 'com.github.johnrengelman.shadow'
|
apply plugin: 'com.github.johnrengelman.shadow'
|
||||||
|
|||||||
@@ -10,20 +10,22 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public abstract class AbstractPromise<T, F> implements Promise<T> {
|
public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||||
|
|
||||||
private final AtomicReference<Collection<PromiseListener<T>>> listeners;
|
private Collection<PromiseListener<T>> listeners;
|
||||||
private final AtomicReference<PromiseCompletion<T>> completion;
|
private final AtomicReference<PromiseCompletion<T>> completion;
|
||||||
private final CountDownLatch latch;
|
private final CountDownLatch latch;
|
||||||
private final ReentrantLock lock;
|
private final Lock lock;
|
||||||
|
|
||||||
public AbstractPromise() {
|
public AbstractPromise() {
|
||||||
this.listeners = new AtomicReference<>();
|
|
||||||
this.completion = new AtomicReference<>();
|
this.completion = new AtomicReference<>();
|
||||||
this.latch = new CountDownLatch(1);
|
this.latch = new CountDownLatch(1);
|
||||||
this.lock = new ReentrantLock();
|
this.lock = new ReentrantLock();
|
||||||
@@ -61,21 +63,31 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T join(long timeoutMillis) throws TimeoutException {
|
public T await(long timeoutMillis) throws TimeoutException {
|
||||||
try {
|
try {
|
||||||
//noinspection ResultOfMethodCallIgnored
|
boolean success = this.latch.await(timeoutMillis, TimeUnit.MILLISECONDS);
|
||||||
this.latch.await(timeoutMillis, TimeUnit.MILLISECONDS);
|
if (!success) {
|
||||||
|
throw new TimeoutException("Promise stopped waiting after " + timeoutMillis + "ms");
|
||||||
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
PromiseCompletion<T> completion = getCompletion();
|
return joinCompletion(Objects.requireNonNull(getCompletion()));
|
||||||
if (completion == null)
|
|
||||||
throw new TimeoutException("Promise stopped waiting after " + timeoutMillis + "ms");
|
|
||||||
|
|
||||||
return joinCompletion(completion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T await() {
|
||||||
|
try {
|
||||||
|
this.latch.await();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return joinCompletion(Objects.requireNonNull(getCompletion()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private T joinCompletion(PromiseCompletion<T> completion) {
|
private T joinCompletion(PromiseCompletion<T> completion) {
|
||||||
if (completion.isError())
|
if (completion.isError())
|
||||||
throw new RuntimeException(completion.getException());
|
throw new RuntimeException(completion.getException());
|
||||||
@@ -315,19 +327,21 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private @NotNull Promise<T> addAnyListener(PromiseListener<T> listener) {
|
private @NotNull Promise<T> addAnyListener(PromiseListener<T> listener) {
|
||||||
|
PromiseCompletion<T> completion;
|
||||||
|
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
PromiseCompletion<T> completion = getCompletion();
|
completion = getCompletion();
|
||||||
if (completion != null) {
|
if (completion == null) {
|
||||||
callListener(listener, completion);
|
if (listeners == null) listeners = new LinkedList<>();
|
||||||
} else {
|
listeners.add(listener);
|
||||||
listeners.compareAndSet(null, new ConcurrentLinkedQueue<>());
|
return this;
|
||||||
listeners.get().add(listener);
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
callListener(listener, completion);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,12 +404,11 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleCompletion(@NotNull PromiseCompletion<T> ctx) {
|
private void handleCompletion(@NotNull PromiseCompletion<T> ctx) {
|
||||||
if (!setCompletion(ctx)) return;
|
|
||||||
|
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
|
if (!setCompletion(ctx)) return;
|
||||||
|
|
||||||
this.latch.countDown();
|
this.latch.countDown();
|
||||||
Collection<PromiseListener<T>> listeners = this.listeners.get();
|
|
||||||
if (listeners != null) {
|
if (listeners != null) {
|
||||||
for (PromiseListener<T> listener : listeners) {
|
for (PromiseListener<T> listener : listeners) {
|
||||||
callListener(listener, ctx);
|
callListener(listener, ctx);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import dev.tommyjs.futur.function.ExceptionalConsumer;
|
|||||||
import dev.tommyjs.futur.function.ExceptionalFunction;
|
import dev.tommyjs.futur.function.ExceptionalFunction;
|
||||||
import dev.tommyjs.futur.function.ExceptionalRunnable;
|
import dev.tommyjs.futur.function.ExceptionalRunnable;
|
||||||
import dev.tommyjs.futur.function.ExceptionalSupplier;
|
import dev.tommyjs.futur.function.ExceptionalSupplier;
|
||||||
|
import org.jetbrains.annotations.Blocking;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -123,7 +124,20 @@ public interface Promise<T> {
|
|||||||
|
|
||||||
void completeExceptionally(@NotNull Throwable result);
|
void completeExceptionally(@NotNull Throwable result);
|
||||||
|
|
||||||
T join(long timeout) throws TimeoutException;
|
@Blocking
|
||||||
|
T await();
|
||||||
|
|
||||||
|
@Blocking
|
||||||
|
T await(long timeout) throws TimeoutException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use await instead.
|
||||||
|
*/
|
||||||
|
@Blocking
|
||||||
|
@Deprecated
|
||||||
|
default T join(long timeout) throws TimeoutException {
|
||||||
|
return await(timeout);
|
||||||
|
};
|
||||||
|
|
||||||
@Nullable PromiseCompletion<T> getCompletion();
|
@Nullable PromiseCompletion<T> getCompletion();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user