Search in sources :

Example 81 with SuspendableRunnable

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

the class InstrumentationOptimizerTest method testDontSkipForwardsWithLoop.

@Test
public void testDontSkipForwardsWithLoop() throws InterruptedException, SuspendExecution, ExecutionException {
    new Fiber(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            dontSkipForwardsWithLoop();
        }
    }).start().join();
    assertFalse(isOptimized("skipForwardsWithLoop"));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 82 with SuspendableRunnable

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

the class InstrumentationOptimizerTest method testSkipForwardsWithMethodAfter.

@Test
public void testSkipForwardsWithMethodAfter() throws InterruptedException, SuspendExecution, ExecutionException {
    new Fiber(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            skipForwardsWithMethodAfter();
        }
    }).start().join();
    assertTrue(isOptimized("skipForwardsWithMethodAfter"));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 83 with SuspendableRunnable

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

the class InstrumentationOptimizerTest method testSkipForwardsToSuspendableInt.

@Test
public void testSkipForwardsToSuspendableInt() throws InterruptedException, SuspendExecution, ExecutionException {
    new Fiber(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            skipForwardsToSuspendableInt();
        }
    }).start().join();
    assertTrue(isOptimized("skipForwardsToSuspendableInt"));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 84 with SuspendableRunnable

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

the class InstrumentationOptimizerTest method testDontSkipForwardsWithTryCatch.

@Test
public void testDontSkipForwardsWithTryCatch() throws InterruptedException, SuspendExecution, ExecutionException {
    new Fiber(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            dontSkipForwardsWithTryCatch();
        }
    }).start().join();
    assertFalse(isOptimized("skipForwardsWithTryCatch"));
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Fiber(co.paralleluniverse.fibers.Fiber) Test(org.junit.Test)

Example 85 with SuspendableRunnable

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

the class Pipeline method parallelTransfer.

private void parallelTransfer() throws SuspendExecution, InterruptedException {
    // 1) Fire workers
    for (int i = 0; i < parallelism; i++) {
        strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

            @Override
            public void run() throws SuspendExecution, InterruptedException {
                // Get first job
                Pair<S, Channel<Channel<T>>> job = jobs.receive();
                while (job != null) {
                    // Build result channel
                    final Channel<T> res = resultChannelBuilder.run();
                    // Process
                    transformer.call(job.getFirst(), res);
                    final Channel<Channel<T>> resWrapper = job.getSecond();
                    // Send result asynchronously
                    strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

                        @Override
                        public void run() throws SuspendExecution, InterruptedException {
                            resWrapper.send(res);
                        }
                    })).start();
                    // Get next job
                    job = jobs.receive();
                }
                // No more jobs, close results channel and quit worker
                results.close();
            }
        })).start();
    }
    // 2) Send jobs asynchronously
    strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            // Get first input
            S s = from.receive();
            while (s != null) {
                final Channel<Channel<T>> resultWrapper = Channels.newChannel(1, Channels.OverflowPolicy.BLOCK, true, true);
                jobs.send(new Pair<>(s, resultWrapper));
                results.send(resultWrapper);
                // Get next input
                s = from.receive();
            }
            // No more inputs, close jobs channel and quit
            jobs.close();
        }
    })).start();
    // 3) Collect and transfer results asynchronously
    try {
        final Strand collector = strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

            @Override
            public void run() throws SuspendExecution, InterruptedException {
                // Get first result
                Channel<Channel<T>> resWrapper = results.receive();
                while (resWrapper != null) {
                    // Get wrapper
                    Channel<T> res = resWrapper.receive();
                    // Get first actual result
                    T out = res.receive();
                    while (out != null) {
                        // Send to output channel
                        to.send(out);
                        // Increment counter
                        transferred.incrementAndGet();
                        // Get next result
                        out = res.receive();
                    }
                    resWrapper = results.receive();
                }
            // No more results, quit
            }
        })).start();
        // TODO solve nasty instrumentation problems on Strand.join()
        if (collector.isFiber()) {
            Fiber f = (Fiber) collector.getUnderlying();
            f.join();
        } else
            collector.join();
    } catch (ExecutionException ee) {
        throw new AssertionError(ee);
    }
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Channel(co.paralleluniverse.strands.channels.Channel) Fiber(co.paralleluniverse.fibers.Fiber) Strand(co.paralleluniverse.strands.Strand) ExecutionException(java.util.concurrent.ExecutionException) Pair(co.paralleluniverse.common.util.Pair)

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