Skip to content

Instantly share code, notes, and snippets.

@marcomorain
Created September 27, 2016 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcomorain/76285cb75a437335372ee54583316a4e to your computer and use it in GitHub Desktop.
Save marcomorain/76285cb75a437335372ee54583316a4e to your computer and use it in GitHub Desktop.
public class Promise<T> {
private final AtomicReference<T> ref = new AtomicReference<T>();
private final CountDownLatch latch = new CountDownLatch(1);
public final void deliver(T object) {
if (this.ref.compareAndSet(null, object)) {
latch.countDown();
}
}
public final T deref() throws InterruptedException {
latch.await();
return ref.get();
}
public final T deref(long timeout, TimeUnit unit) throws InterruptedException {
latch.await(timeout, unit);
return ref.get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment