eu.lucaventuri.concurrent.SignalingSingleConsumer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fibry Show documentation
Show all versions of fibry Show documentation
The first Java Actor System supporting fibers from Project Loom
package eu.lucaventuri.concurrent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/** Runnable that can run only once, and that can signal when it has run */
public class SignalingSingleConsumer implements Consumer {
private final CountDownLatch latch = new CountDownLatch(1);
private final Consumer worker;
private SignalingSingleConsumer(Consumer worker) {
this.worker = worker;
}
@Override
public void accept(S object) {
assert worker != null;
if (worker != null)
worker.accept(object);
latch.countDown();
}
public void await() throws InterruptedException {
latch.await();
}
public void await(long timeout, TimeUnit unit) throws InterruptedException {
latch.await(timeout, unit);
}
public boolean isDone() {
return latch.getCount() == 0;
}
public static SignalingSingleConsumer of(Consumer worker) {
if (worker instanceof SignalingSingleConsumer)
return (SignalingSingleConsumer) worker;
return new SignalingSingleConsumer(worker);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy