Package co.paralleluniverse.fibers

Examples of co.paralleluniverse.fibers.Fiber


    public void testSelectSendWithClose1() throws Exception {
        final Channel<String> channel1 = newChannel();
        final Channel<String> channel2 = newChannel();
        final Channel<String> channel3 = newChannel();

        Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution, InterruptedException {
                SelectAction<String> sa1 = select(
                        send(channel1, "hi1"),
                        send(channel2, "hi2"),
                        send(channel3, "hi3"));

                SelectAction<String> sa2 = select(
                        send(channel1, "hi1"),
                        send(channel2, "hi2"),
                        send(channel3, "hi3"));

                assertThat(sa1.index(), is(1));
                assertThat(sa2.index(), is(1));
            }
        }).start();

        Thread.sleep(200);

        channel2.close();

        fib.join();
    }
View Full Code Here


    public void testSelectSendWithClose2() throws Exception {
        final Channel<String> channel1 = newChannel();
        final Channel<String> channel2 = newChannel();
        final Channel<String> channel3 = newChannel();

        Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution, InterruptedException {
                Strand.sleep(200);
                SelectAction<String> sa1 = select(
                        send(channel1, "hi1"),
                        send(channel2, "hi2"),
                        send(channel3, "hi3"));

                Strand.sleep(200);
                SelectAction<String> sa2 = select(
                        send(channel1, "hi1"),
                        send(channel2, "hi2"),
                        send(channel3, "hi3"));

                assertThat(sa1.index(), is(1));
                assertThat(sa2.index(), is(1));
            }
        }).start();


        channel2.close();

        fib.join();
    }
View Full Code Here

    public void testSelectSendTimeout() throws Exception {
        final Channel<String> channel1 = newChannel();
        final Channel<String> channel2 = newChannel();
        final Channel<String> channel3 = newChannel();

        Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution, InterruptedException {
                SelectAction<String> sa1 = select(1, TimeUnit.MILLISECONDS,
                        send(channel1, "hi1"),
                        send(channel2, "hi2"),
                        send(channel3, "hi3"));

                SelectAction<String> sa2 = select(300, TimeUnit.MILLISECONDS,
                        send(channel1, "bye1"),
                        send(channel2, "bye2"),
                        send(channel3, "bye3"));

                assertThat(sa1, is(nullValue()));
                assertThat(sa2.index(), is(2));
            }
        }).start();

        Thread.sleep(200);

        String m1 = channel3.receive();

        assertThat(m1, equalTo("bye3")); // the first send is cancelled

        fib.join();
    }
View Full Code Here

    public void testSelectSendWithTimeoutChannel() throws Exception {
        final Channel<String> channel1 = newChannel();
        final Channel<String> channel2 = newChannel();
        final Channel<String> channel3 = newChannel();

        Fiber fib = new Fiber("fiber", scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution, InterruptedException {
                SelectAction<String> sa1 = select(
                        send(channel1, "hi1"),
                        send(channel2, "hi2"),
                        send(channel3, "hi3"),
                        receive(TimeoutChannel.<String>timeout(1, TimeUnit.MILLISECONDS)));

                SelectAction<String> sa2 = select(
                        send(channel1, "bye1"),
                        send(channel2, "bye2"),
                        send(channel3, "bye3"),
                        receive(TimeoutChannel.<String>timeout(300, TimeUnit.MILLISECONDS)));

                assertThat(sa1.index(), is(3));
                assertThat(sa2.index(), is(2));
            }
        }).start();

        Thread.sleep(200);

        String m1 = channel3.receive();

        assertThat(m1, equalTo("bye3")); // the first send is cancelled

        fib.join();
    }
View Full Code Here

        return Channels.newChannel(mailboxSize, policy, singleProducer, singleConsumer);
    }

    void spawn(SuspendableRunnable r) {
        if (fiber)
            new Fiber(scheduler, r).start();
        else
            new Thread(Strand.toRunnable(r)).start();
    }
View Full Code Here

            throw new IllegalStateException("A strand can only be cloned after death. " + strand + " isn't dead.");
        if (strand instanceof FiberStrand)
            return clone((Fiber) strand.getUnderlying(), target);

        if (strand instanceof Fiber)
            return new Fiber((Fiber) strand, target);
        else
            return ThreadStrand.get(cloneThread((Thread) strand.getUnderlying(), toRunnable(target)));
    }
View Full Code Here

            throw new IllegalStateException("A strand can only be cloned after death. " + strand + " isn't dead.");
        if (strand instanceof FiberStrand)
            return clone((Fiber) strand.getUnderlying(), target);

        if (strand instanceof Fiber)
            return new Fiber((Fiber) strand, target);
        else
            return ThreadStrand.get(cloneThread((Thread) strand.getUnderlying(), toRunnable(target)));
    }
View Full Code Here

        int i = 1;

        for (; i < 50; i++)
            sch.send(i);
        Fiber f1 = new Fiber(scheduler, run).start();
        Thread t1 = new Thread(Strand.toRunnable(run));
        t1.start();
        for (; i < 200; i++)
            sch.send(i);
        Fiber f2 = new Fiber(scheduler, run).start();
        Thread t2 = new Thread(Strand.toRunnable(run));
        t2.start();
        for (; i < 600; i++)
            sch.send(i);
        Fiber f3 = new Fiber(scheduler, run).start();
        Thread t3 = new Thread(Strand.toRunnable(run));
        t3.start();
        for (; i < 800; i++)
            sch.send(i);
        Fiber f4 = new Fiber(scheduler, run).start();
        Thread t4 = new Thread(Strand.toRunnable(run));
        t4.start();
        for (; i < 2000; i++)
            sch.send(i);

        sch.close();
        System.out.println("done send");

        Debug.dumpAfter(5000, "ticker.log");
        f1.join();
        System.out.println("f1: " + f1);
        f2.join();
        System.out.println("f2: " + f2);
        f3.join();
        System.out.println("f3: " + f3);
        f4.join();
        System.out.println("f4: " + f4);
        t1.join();
        System.out.println("t1: " + t1);
        t2.join();
        System.out.println("t2: " + t2);
View Full Code Here

    Strand newStrand(boolean heavyweight, SuspendableRunnable target) {
        if (heavyweight)
            return Strand.of(new Thread(Strand.toRunnable(target)));
        else
            return Strand.of(new Fiber(target));
    }
View Full Code Here

    @Test
    public void testFilterFiberToFiber() throws Exception {
        final Channel<Integer> ch = newChannel();

        Fiber fib1 = new Fiber("fiber", scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution, InterruptedException {
                ReceivePort<Integer> ch1 = Channels.filter((ReceivePort<Integer>) ch, new Predicate<Integer>() {
                    @Override
                    public boolean apply(Integer input) {
                        return input % 2 == 0;
                    }
                });

                Integer m1 = ch1.receive();
                Integer m2 = ch1.receive();
                Integer m3 = ch1.receive();

                assertThat(m1, equalTo(2));
                assertThat(m2, equalTo(4));
                assertThat(m3, is(nullValue()));
            }
        }).start();

        Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution, InterruptedException {
                Strand.sleep(50);
                ch.send(1);
                ch.send(2);
                Strand.sleep(50);
                ch.send(3);
                ch.send(4);
                ch.send(5);
                ch.close();
            }
        }).start();

        fib1.join();
        fib2.join();
    }
View Full Code Here

TOP

Related Classes of co.paralleluniverse.fibers.Fiber

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.