use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class FiberOverheadJMHBenchmark method preapre.
@Setup
public void preapre() {
rands = new long[(DEPTH + 1) * 4];
Random rnd = ThreadLocalRandom.current();
for (int i = 0; i < rands.length; i++) rands[i] = rnd.nextLong();
runnable = new Runnable() {
@Override
public void run() {
res = recursive1(DEPTH);
}
};
fiber = new Fiber((String) null, null, STACK, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution {
res = recursive2(DEPTH);
}
});
fiber2 = new Fiber((String) null, null, STACK, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution {
res = recursive3(DEPTH);
}
});
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class FiberTest method whenFiberIsNewThenDumpStackReturnsNull.
@Test
public void whenFiberIsNewThenDumpStackReturnsNull() throws Exception {
Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
foo();
}
private void foo() {
}
});
StackTraceElement[] st = fiber.getStackTrace();
assertThat(st, is(nullValue()));
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class FiberTest method testDumpStackWaitingFiber.
@Test
public void testDumpStackWaitingFiber() throws Exception {
final Condition cond = new SimpleConditionSynchronizer(null);
final AtomicBoolean flag = new AtomicBoolean(false);
Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
foo();
}
private void foo() throws InterruptedException, SuspendExecution {
Object token = cond.register();
try {
for (int i = 0; !flag.get(); i++) cond.await(i);
} finally {
cond.unregister(token);
}
}
}).start();
Thread.sleep(200);
StackTraceElement[] st = fiber.getStackTrace();
// Strand.printStackTrace(st, System.err);
assertThat(st[0].getMethodName(), equalTo("park"));
boolean found = false;
for (StackTraceElement ste : st) {
if (ste.getMethodName().equals("foo")) {
found = true;
break;
}
}
assertThat(found, is(true));
assertThat(st[st.length - 1].getMethodName(), equalTo("run"));
assertThat(st[st.length - 1].getClassName(), equalTo(Fiber.class.getName()));
flag.set(true);
cond.signalAll();
fiber.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class FiberTest method testThreadLocalsParallel.
@Test
public void testThreadLocalsParallel() throws Exception {
final ThreadLocal<String> tl = new ThreadLocal<>();
final int n = 100;
final int loops = 100;
Fiber[] fibers = new Fiber[n];
for (int i = 0; i < n; i++) {
final int id = i;
Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
for (int j = 0; j < loops; j++) {
final String tlValue = "tl-" + id + "-" + j;
tl.set(tlValue);
assertThat(tl.get(), equalTo(tlValue));
Strand.sleep(10);
assertThat(tl.get(), equalTo(tlValue));
}
}
});
fiber.start();
fibers[i] = fiber;
}
for (Fiber fiber : fibers) fiber.join();
}
use of co.paralleluniverse.strands.SuspendableRunnable in project quasar by puniverse.
the class FiberTest method testCancel2.
@Test
public void testCancel2() throws Exception {
final AtomicBoolean started = new AtomicBoolean();
final AtomicBoolean terminated = new AtomicBoolean();
Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution {
started.set(true);
try {
Fiber.sleep(100);
fail("InterruptedException not thrown");
} catch (InterruptedException e) {
}
terminated.set(true);
}
});
fiber.cancel(true);
fiber.start();
Thread.sleep(20);
try {
fiber.join(5, TimeUnit.MILLISECONDS);
fail();
} catch (CancellationException e) {
}
assertThat(started.get(), is(false));
assertThat(terminated.get(), is(false));
}
Aggregations