Created
September 27, 2016 11:36
-
-
Save marcomorain/76285cb75a437335372ee54583316a4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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