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