Search in sources :

Example 6 with Suspendable

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

the class ProxyServerTest method testCast.

@Test
public void testCast() throws Exception {
    final AtomicInteger called = new AtomicInteger(0);
    final Server<?, ?, ?> a = spawnServer(false, new A() {

        public int foo(String str, int x) {
            throw new UnsupportedOperationException();
        }

        @Suspendable
        public void bar(int x) {
            try {
                Strand.sleep(100);
                called.set(x);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (SuspendExecution e) {
                throw new AssertionError(e);
            }
        }
    });
    ((A) a).bar(15);
    assertThat(called.get(), is(0));
    Thread.sleep(200);
    assertThat(called.get(), is(15));
    a.shutdown();
    LocalActor.join(a, 100, TimeUnit.MILLISECONDS);
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Suspendable(co.paralleluniverse.fibers.Suspendable) Test(org.junit.Test)

Example 7 with Suspendable

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

the class AbstractFuture method get.

@Override
@Suspendable
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    try {
        if (done)
            return getValue();
        long left = unit.toNanos(timeout);
        final long deadline = System.nanoTime() + left;
        Object token = sync.register();
        try {
            for (int i = 0; !done; i++) {
                sync.await(i, left, TimeUnit.NANOSECONDS);
                left = deadline - System.nanoTime();
                if (left <= 0)
                    throw new TimeoutException();
            }
        } finally {
            sync.unregister(token);
        }
        return getValue();
    } catch (SuspendExecution e) {
        throw new AssertionError(e);
    }
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) TimeoutException(java.util.concurrent.TimeoutException) Suspendable(co.paralleluniverse.fibers.Suspendable)

Aggregations

Suspendable (co.paralleluniverse.fibers.Suspendable)7 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)5 TimeoutException (java.util.concurrent.TimeoutException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 TestInterface (co.paralleluniverse.common.test.TestInterface)1 TestInterface2 (co.paralleluniverse.common.test.TestInterface2)1 RuntimeExecutionException (co.paralleluniverse.fibers.RuntimeExecutionException)1 SimpleConditionSynchronizer (co.paralleluniverse.strands.SimpleConditionSynchronizer)1 IOException (java.io.IOException)1 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutorService (java.util.concurrent.ExecutorService)1