Search in sources :

Example 1 with SuspendableCallable

use of co.paralleluniverse.strands.SuspendableCallable 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 2 with SuspendableCallable

use of co.paralleluniverse.strands.SuspendableCallable in project useful-java-links by Vedenin.

the class FibersAndChanelHelloWorld method doTest.

public static Integer doTest() throws Exception {
    // Synchronizing channel (buffer = 0)
    final IntChannel fiber1ToFiber2 = Channels.newIntChannel(0);
    // Synchronizing channel (buffer = 0)
    final IntChannel fiber2ToFiber1 = Channels.newIntChannel(0);
    Fiber<Integer> fiber1 = new Fiber<>("Fiber1", new SuspendableCallable<Integer>() {

        @Override
        public Integer run() throws SuspendExecution, InterruptedException {
            Fiber.sleep(1000);
            fiber1ToFiber2.send(1);
            Integer i1 = fiber2ToFiber1.receive();
            System.out.println(" Hello words " + i1);
            fiber1ToFiber2.send(9);
            Integer i2 = fiber2ToFiber1.receive();
            System.out.println(" Hello words " + i2);
            fiber1ToFiber2.send(0);
            return i2;
        }
    }).start();
    Fiber<Void> fiber2 = new Fiber<Void>("Fiber2", new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            Integer i;
            i = fiber1ToFiber2.receive();
            while (i != 0) {
                fiber2ToFiber1.send(i + 1);
                i = fiber1ToFiber2.receive();
            }
        }
    }).start();
    fiber1.join();
    fiber2.join();
    return fiber1.get();
}
Also used : IntChannel(co.paralleluniverse.strands.channels.IntChannel) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) SuspendableCallable(co.paralleluniverse.strands.SuspendableCallable)

Example 3 with SuspendableCallable

use of co.paralleluniverse.strands.SuspendableCallable in project quasar by puniverse.

the class FiniteStateMachineTest method testStates.

@Test
public void testStates() throws Exception {
    final AtomicBoolean success = new AtomicBoolean();
    ActorRef<Object> a = new FiniteStateMachineActor() {

        @Override
        protected SuspendableCallable<SuspendableCallable> initialState() {
            return new SuspendableCallable<SuspendableCallable>() {

                public SuspendableCallable run() throws SuspendExecution, InterruptedException {
                    return state1();
                }
            };
        }

        private SuspendableCallable<SuspendableCallable> state1() throws SuspendExecution, InterruptedException {
            return receive(new MessageProcessor<Object, SuspendableCallable<SuspendableCallable>>() {

                @Override
                public SuspendableCallable<SuspendableCallable> process(Object m) throws SuspendExecution, InterruptedException {
                    if ("a".equals(m))
                        return new SuspendableCallable<SuspendableCallable>() {

                            public SuspendableCallable run() throws SuspendExecution, InterruptedException {
                                return state2();
                            }
                        };
                    return null;
                }
            });
        }

        private SuspendableCallable<SuspendableCallable> state2() throws SuspendExecution, InterruptedException {
            return receive(new MessageProcessor<Object, SuspendableCallable<SuspendableCallable>>() {

                @Override
                public SuspendableCallable<SuspendableCallable> process(Object m) throws SuspendExecution, InterruptedException {
                    if ("b".equals(m)) {
                        success.set(true);
                        return TERMINATE;
                    }
                    return null;
                }
            });
        }
    }.spawn();
    a.send("b");
    a.send("a");
    LocalActor.join(a, 100, TimeUnit.MILLISECONDS);
    assertTrue(success.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) MessageProcessor(co.paralleluniverse.actors.MessageProcessor) SuspendableCallable(co.paralleluniverse.strands.SuspendableCallable) Test(org.junit.Test)

Example 4 with SuspendableCallable

use of co.paralleluniverse.strands.SuspendableCallable in project dubbo-faker by moyada.

the class FiberInvoker method invoke.

@Suspendable
@Override
public void invoke(Object[] argsValue) {
    super.count.increment();
    Timestamp invokeTime = Timestamp.from(Instant.now());
    Fiber<FutureResult> fiber = this.scheduler.newFiber((SuspendableCallable<FutureResult>) () -> {
        FutureResult result;
        long start = System.nanoTime();
        try {
            result = FutureResult.success(execute(argsValue));
        } catch (Throwable e) {
            result = FutureResult.failed(e.getMessage());
        }
        result.setSpend((System.nanoTime() - start) / 1000_000);
        return result;
    }).start();
    for (; ; ) {
        if (fiber.isDone()) {
            try {
                FutureResult result = fiber.get();
                super.callback(new InvokeFuture(result, invokeTime, Arrays.toString(argsValue)));
            } catch (ExecutionException | InterruptedException e) {
                e.printStackTrace();
            }
            super.count.decrement();
            break;
        }
    }
}
Also used : FutureResult(cn.moyada.dubbo.faker.core.model.FutureResult) InvokeFuture(cn.moyada.dubbo.faker.core.model.InvokeFuture) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(java.sql.Timestamp) SuspendableCallable(co.paralleluniverse.strands.SuspendableCallable) Suspendable(co.paralleluniverse.fibers.Suspendable)

Aggregations

SuspendableCallable (co.paralleluniverse.strands.SuspendableCallable)4 FutureResult (cn.moyada.dubbo.faker.core.model.FutureResult)1 InvokeFuture (cn.moyada.dubbo.faker.core.model.InvokeFuture)1 MessageProcessor (co.paralleluniverse.actors.MessageProcessor)1 Fiber (co.paralleluniverse.fibers.Fiber)1 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)1 Suspendable (co.paralleluniverse.fibers.Suspendable)1 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)1 IntChannel (co.paralleluniverse.strands.channels.IntChannel)1 Timestamp (java.sql.Timestamp)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1