Search in sources :

Example 1 with RuntimeExecutionException

use of co.paralleluniverse.fibers.RuntimeExecutionException in project quasar by puniverse.

the class Val method get.

/**
 * Returns the delayed value, blocking until it has been set, but no longer than the given timeout.
 *
 * @param timeout The maximum duration to block waiting for the value to be set.
 * @param unit    The time unit of the timeout value.
 * @return the value
 * @throws TimeoutException     if the timeout expires before the value is set.
 * @throws InterruptedException
 */
@Override
@Suspendable
public V get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
    try {
        final SimpleConditionSynchronizer s = sync;
        if (s != null) {
            Object token = s.register();
            try {
                final long start = System.nanoTime();
                long left = unit.toNanos(timeout);
                final long deadline = start + left;
                for (int i = 0; sync != null; i++) {
                    s.awaitNanos(i, left);
                    if (sync == null)
                        break;
                    left = deadline - System.nanoTime();
                    if (left <= 0)
                        throw new TimeoutException();
                }
            } finally {
                s.unregister(token);
            }
        }
        if (t != null)
            throw t instanceof CancellationException ? (CancellationException) t : new RuntimeExecutionException(t);
        return value;
    } catch (SuspendExecution e) {
        throw new AssertionError(e);
    }
}
Also used : SimpleConditionSynchronizer(co.paralleluniverse.strands.SimpleConditionSynchronizer) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) RuntimeExecutionException(co.paralleluniverse.fibers.RuntimeExecutionException) Suspendable(co.paralleluniverse.fibers.Suspendable)

Aggregations

RuntimeExecutionException (co.paralleluniverse.fibers.RuntimeExecutionException)1 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)1 Suspendable (co.paralleluniverse.fibers.Suspendable)1 SimpleConditionSynchronizer (co.paralleluniverse.strands.SimpleConditionSynchronizer)1 CancellationException (java.util.concurrent.CancellationException)1 TimeoutException (java.util.concurrent.TimeoutException)1