Package co.paralleluniverse.fibers

Examples of co.paralleluniverse.fibers.Fiber


    final void interrupt() {
        getStrand().interrupt();
    }

    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 || !(target instanceof Actor))
            return null;
        return (Actor<M, V>) target;
    }
View Full Code Here


    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;
    }
View Full Code Here

    }

    public static ParkableForkJoinTask<?> getCurrent() {
        ParkableForkJoinTask ct = getCurrent1();
        if (ct == null && Thread.currentThread() instanceof ForkJoinWorkerThread) { // false in tests
            Fiber f = Fiber.currentFiber();
            if (f != null)
                ct = (ParkableForkJoinTask) f.getTask();
        }
        return ct;
    }
View Full Code Here

    private EventSourceActor<String> spawnGenEvent(Initializer initializer) {
        return spawnActor(new EventSourceActor<String>(initializer));
    }

    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;
    }
View Full Code Here

        return spawnActor(new LocalGenEvent<String>(initializer));
    }
    static private ForkJoinPool fjPool = new ForkJoinPool(4, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true);

    private <T extends LocalActor<Message, V>, Message, V> T spawnActor(T actor) {
        Fiber fiber = new Fiber(fjPool, actor);
        fiber.setUncaughtExceptionHandler(new Fiber.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Fiber lwt, Throwable e) {
                e.printStackTrace();
                throw Exceptions.rethrow(e);
            }
        });
        fiber.start();
        return actor;


    }
View Full Code Here

    public void tearDown() {
    }

    @Test
    public void testFiberAsyncSocket() throws Exception {
        final Fiber server = new Fiber(scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution {
                try (FiberServerSocketChannel socket = FiberServerSocketChannel.open().bind(new InetSocketAddress(PORT));
                        FiberSocketChannel ch = socket.accept()) {

                    ByteBuffer buf = ByteBuffer.allocateDirect(1024);

                    // long-typed reqeust/response
                    int n = ch.read(buf);

                    assertThat(n, is(8)); // we assume the message is sent in a single packet

                    buf.flip();
                    long req = buf.getLong();

                    assertThat(req, is(12345678L));

                    buf.clear();
                    long res = 87654321L;
                    buf.putLong(res);
                    buf.flip();

                    n = ch.write(buf);

                    assertThat(n, is(8));

                    // String reqeust/response
                    buf.clear();
                    n = ch.read(buf); // we assume the message is sent in a single packet

                    buf.flip();
                    String req2 = decoder.decode(buf).toString();

                    assertThat(req2, is("my request"));

                    String res2 = "my response";
                    n = ch.write(encoder.encode(CharBuffer.wrap(res2)));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        });

        final Fiber client = new Fiber(scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution {
                try (FiberSocketChannel ch = FiberSocketChannel.open(new InetSocketAddress(PORT))) {
                    ByteBuffer buf = ByteBuffer.allocateDirect(1024);

                    // long-typed reqeust/response
                    long req = 12345678L;
                    buf.putLong(req);
                    buf.flip();

                    int n = ch.write(buf);

                    assertThat(n, is(8));

                    buf.clear();
                    n = ch.read(buf);

                    assertThat(n, is(8)); // we assume the message is sent in a single packet

                    buf.flip();
                    long res = buf.getLong();

                    assertThat(res, is(87654321L));

                    // String reqeust/response
                    String req2 = "my request";
                    n = ch.write(encoder.encode(CharBuffer.wrap(req2)));

                    buf.clear();
                    n = ch.read(buf); // we assume the message is sent in a single packet

                    buf.flip();
                    String res2 = decoder.decode(buf).toString();

                    assertThat(res2, is("my response"));

                    // verify that the server has closed the socket
                    buf.clear();
                    n = ch.read(buf);

                    assertThat(n, is(-1));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        server.start();
        Thread.sleep(100);
        client.start();

        client.join();
        server.join();
    }
View Full Code Here

        server.join();
    }

    @Test
    public void testFiberAsyncFile() throws Exception {
        new Fiber(scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution {
                try (FiberFileChannel ch = FiberFileChannel.open(Paths.get(System.getProperty("user.home"), "fibertest.bin"), READ, WRITE, CREATE, TRUNCATE_EXISTING)) {
                    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
                   
View Full Code Here

    public static ExecutorService ioExecutor(FiberScheduler scheduler) {
        return protectScheduler(scheduler);
    }

    public static ExecutorService ioExecutor() {
        Fiber currentFiber = Fiber.currentFiber();
        if (currentFiber == null)
            throw new IllegalStateException("Method called not within a fiber");
        return ioExecutor(currentFiber.getScheduler());
    }
View Full Code Here

     * not yet been returned), unless this Var does not yet have a value; only in that case will this method block.
     */
    public T get() throws SuspendExecution, InterruptedException {
        TLVar tl = tlv.get();
        if (tl.type == UNKNOWN) {
            Fiber currentFiber = Fiber.currentFiber();
            if (currentFiber != null && currentFiber instanceof VarFiber) {
                final VarFiber<?> vf = (VarFiber<?>) currentFiber;
                tl.type = VARFIBER;
                registeredFibers.add(vf);
                vf.registeredVars.add(this);
View Full Code Here

                out.send(1234);
                out.close();
            }
        });

        Fiber fib1 = new Fiber("fiber", scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution, InterruptedException {
                assertThat(out.receive(), equalTo(20));
                assertThat(out.receive(), equalTo(40));
                assertThat(out.receive(), equalTo(1234));
                assertThat(out.receive(), is(nullValue()));
            }
        }).start();

        Fiber fib2 = new Fiber("fiber", scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution, InterruptedException {
                Strand.sleep(50);
                in.send(1);
                in.send(2);
                Strand.sleep(50);
                in.send(3);
                in.send(4);
                in.send(5);
                in.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.