V result = null; while (result == null) { timeout.await(latch); result = ref.getAndSet(null);
/** * Gets the current element if it is not null, if it is null then this method * blocks and waits until it is not null. Unlike {@link #take()} it does not * reset the current element to null. * <p> * If the current thread: * <ul> * <li>has its interrupted status set on entry to this method; or * <li>is {@link Thread#interrupt() interrupted} while waiting, * </ul> * then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * * @return the current element * @throws TimeoutException if the timeout is reached without another thread * having called {@link #set(Object)}. * @throws InterruptedException if the current thread is interrupted while * waiting */ @NotNull public V get(final long time, @NotNull final TimeUnit unit) throws TimeoutException, InterruptedException { final Timeout timeout = Timeout.getNanosTimeout(time, unit); V result = ref.get(); while (result == null) { timeout.await(latch); result = ref.get(); } return result; }