Search in sources :

Example 1 with Fiber

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

the class PeerTKB method spawnActor.

private <T extends Actor<Message, V>, Message, V> T spawnActor(T actor) {
    Fiber fiber = new Fiber(actor);
    fiber.setUncaughtExceptionHandler(new Strand.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Strand s, Throwable e) {
            e.printStackTrace();
            throw Exceptions.rethrow(e);
        }
    });
    fiber.start();
    return actor;
}
Also used : Fiber(co.paralleluniverse.fibers.Fiber) Strand(co.paralleluniverse.strands.Strand)

Example 2 with Fiber

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

the class PrimitiveChannelRingBenchmark method run.

void run() throws ExecutionException, InterruptedException {
    final long start = System.nanoTime();
    final IntChannel managerChannel = Channels.newIntChannel(mailboxSize);
    IntChannel a = managerChannel;
    for (int i = 0; i < N - 1; i++) a = createRelayActor(a);
    final IntChannel lastChannel = a;
    Fiber<Integer> manager = new Fiber<Integer>() {

        @Override
        protected Integer run() throws InterruptedException, SuspendExecution {
            // start things off
            lastChannel.send(1);
            int msg = 0;
            try {
                for (int i = 0; i < M; i++) {
                    msg = managerChannel.receiveInt();
                    lastChannel.send(msg + 1);
                }
                return msg;
            } catch (ReceivePort.EOFException e) {
                return null;
            }
        }
    };
    // managerChannel.setStrand(manager);
    manager.start();
    int totalCount = manager.get();
    final long time = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    System.out.println("Messages: " + totalCount + " Time (ms): " + time);
}
Also used : IntChannel(co.paralleluniverse.strands.channels.IntChannel) ReceivePort(co.paralleluniverse.strands.channels.ReceivePort) Fiber(co.paralleluniverse.fibers.Fiber)

Example 3 with Fiber

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

the class SupervisorActor method createStrandForActor.

private Strand createStrandForActor(Strand oldStrand, Actor actor) {
    final Strand strand;
    if (oldStrand != null)
        strand = Strand.clone(oldStrand, actor);
    else
        strand = new Fiber(actor);
    actor.setStrand(strand);
    return strand;
}
Also used : Fiber(co.paralleluniverse.fibers.Fiber) Strand(co.paralleluniverse.strands.Strand)

Example 4 with Fiber

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

the class Actor method currentActor.

/**
 * Returns the actor currently running in the current strand.
 */
public static <M, V> Actor<M, V> currentActor() {
    final Fiber currentFiber = Fiber.currentFiber();
    if (currentFiber == null)
        return currentActor.get();
    final SuspendableCallable target = currentFiber.getTarget();
    if (target == null)
        return null;
    if (target instanceof Actor)
        return (Actor<M, V>) target;
    if (target instanceof ActorRunner)
        return (Actor<M, V>) ((ActorRunner<V>) target).getActor();
    return null;
}
Also used : Fiber(co.paralleluniverse.fibers.Fiber) SuspendableCallable(co.paralleluniverse.strands.SuspendableCallable)

Example 5 with Fiber

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

the class ChannelTest method testChannelGroupReceiveWithTimeout.

@Test
public void testChannelGroupReceiveWithTimeout() throws Exception {
    final Channel<String> channel1 = newChannel();
    final Channel<String> channel2 = newChannel();
    final Channel<String> channel3 = newChannel();
    final ReceivePortGroup<String> group = new ReceivePortGroup<>(channel1, channel2, channel3);
    Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            String m1 = group.receive();
            String m2 = channel2.receive();
            String m3 = group.receive(10, TimeUnit.MILLISECONDS);
            String m4 = group.receive(200, TimeUnit.MILLISECONDS);
            assertThat(m1, equalTo("hello"));
            assertThat(m2, equalTo("world!"));
            assertThat(m3, nullValue());
            assertThat(m4, equalTo("foo"));
        }
    }).start();
    Thread.sleep(100);
    channel3.send("hello");
    Thread.sleep(100);
    channel2.send("world!");
    Thread.sleep(100);
    channel1.send("foo");
    fib.join();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Aggregations

Fiber (co.paralleluniverse.fibers.Fiber)105 Test (org.junit.Test)88 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)20 Strand (co.paralleluniverse.strands.Strand)8 IntChannel (co.paralleluniverse.strands.channels.IntChannel)4 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 ExecutionException (java.util.concurrent.ExecutionException)3 Ignore (org.junit.Ignore)3 Timeout (co.paralleluniverse.strands.Timeout)2 Channel (co.paralleluniverse.strands.channels.Channel)2 ReceivePort (co.paralleluniverse.strands.channels.ReceivePort)2 TimeoutException (java.util.concurrent.TimeoutException)2 ActorRef (co.paralleluniverse.actors.ActorRef)1 AbstractServerHandler (co.paralleluniverse.actors.behaviors.AbstractServerHandler)1 ServerActor (co.paralleluniverse.actors.behaviors.ServerActor)1 Function2 (co.paralleluniverse.common.util.Function2)1 Pair (co.paralleluniverse.common.util.Pair)1 FiberScheduler (co.paralleluniverse.fibers.FiberScheduler)1 FiberWriter (co.paralleluniverse.fibers.FiberWriter)1