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;
}
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();
}
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());
}
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;
}
}
}
Aggregations