Search in sources :

Example 1 with SuspendableRunnable

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);
        }
    });
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Random(java.util.Random) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable)

Example 2 with SuspendableRunnable

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()));
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Test(org.junit.Test)

Example 3 with SuspendableRunnable

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();
}
Also used : Condition(co.paralleluniverse.strands.Condition) SimpleConditionSynchronizer(co.paralleluniverse.strands.SimpleConditionSynchronizer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Test(org.junit.Test)

Example 4 with SuspendableRunnable

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();
}
Also used : SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Test(org.junit.Test)

Example 5 with SuspendableRunnable

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));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) CancellationException(java.util.concurrent.CancellationException) Test(org.junit.Test)

Aggregations

SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)90 Test (org.junit.Test)82 Fiber (co.paralleluniverse.fibers.Fiber)73 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Strand (co.paralleluniverse.strands.Strand)4 QueueCapacityExceededException (co.paralleluniverse.strands.queues.QueueCapacityExceededException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Ignore (org.junit.Ignore)3 Condition (co.paralleluniverse.strands.Condition)2 SimpleConditionSynchronizer (co.paralleluniverse.strands.SimpleConditionSynchronizer)2 Timeout (co.paralleluniverse.strands.Timeout)2 Channel (co.paralleluniverse.strands.channels.Channel)2 IntChannel (co.paralleluniverse.strands.channels.IntChannel)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CheckedCallable (co.paralleluniverse.common.util.CheckedCallable)1 Function2 (co.paralleluniverse.common.util.Function2)1 Pair (co.paralleluniverse.common.util.Pair)1 VerifyInstrumentationException (co.paralleluniverse.fibers.VerifyInstrumentationException)1