Package co.paralleluniverse.strands

Examples of co.paralleluniverse.strands.SimpleConditionSynchronizer


        fiber.join();
    }

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


        fiber.join();
    }

    @Test
    public void testDumpStackWaitingFiberWhenCalledFromFiber() throws Exception {
        final Condition cond = new SimpleConditionSynchronizer(null);
        final AtomicBoolean flag = new AtomicBoolean(false);

        final 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);

        Fiber fiber2 = new Fiber(scheduler, new SuspendableRunnable() {
            @Override
            public void run() throws SuspendExecution, InterruptedException {
                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()));
            }
        }).start();

        fiber2.join();

        flag.set(true);
        cond.signalAll();

        fiber.join();
    }
View Full Code Here

    private boolean receiveClosed;

    protected QueueChannel(BasicQueue<Message> queue, OverflowPolicy overflowPolicy, boolean singleConsumer) {
        this.queue = queue;
        if (!singleConsumer || queue instanceof CircularBuffer)
            this.sync = new SimpleConditionSynchronizer(this);
        else
            this.sync = new OwnedSynchronizer(this);

        this.overflowPolicy = overflowPolicy;
        this.sendersSync = overflowPolicy == OverflowPolicy.BLOCK ? new SimpleConditionSynchronizer(this) : null;
    }
View Full Code Here

            throw new IllegalStateException("Cannot set a value because a function has been set");
        setException0(t);
    }

    private void set0(V value) {
        final SimpleConditionSynchronizer s = sync;
        if (s == null)
            throw new IllegalStateException("Value has already been set (and can only be set once)");
        this.value = value;
        sync = null; // must be done before signal
        this.f = null;
        s.signalAll();
    }
View Full Code Here

        this.f = null;
        s.signalAll();
    }

    private void setException0(Throwable t) {
        final SimpleConditionSynchronizer s = sync;
        if (s == null)
            throw new IllegalStateException("Value has already been set (and can only be set once)");
        this.t = t;
        sync = null; // must be done before signal
        this.f = null;
        s.signalAll();
    }
View Full Code Here

     */
    @Override
    @Suspendable
    public V get() throws InterruptedException {
        try {
            final SimpleConditionSynchronizer s = sync;
            if (s != null) {
                Object token = s.register();
                try {
                    for (int i = 0; sync != null; i++)
                        s.await(i);
                } finally {
                    s.unregister(token);
                }
            }
            if (t != null)
                throw new RuntimeExecutionException(t);
            return value;
View Full Code Here

     */
    @Override
    @Suspendable
    public V get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
        try {
            final SimpleConditionSynchronizer s = sync;
            if (s != null) {
                Object token = s.register();
                try {
                    final long start = System.nanoTime();
                    long left = unit.toNanos(timeout);
                    final long deadline = start + left;
                    for (int i = 0; sync != null; i++) {
                        s.awaitNanos(i, left);
                        if (sync == null)
                            break;
                        left = deadline - System.nanoTime();
                        if (left <= 0)
                            throw new TimeoutException();
                    }
                } finally {
                    s.unregister(token);
                }
            }
            if (t != null)
                throw t instanceof CancellationException ? (CancellationException) t : new RuntimeExecutionException(t);
            return value;
View Full Code Here

    private boolean receiveClosed;

    protected QueueChannel(BasicQueue<Message> queue, OverflowPolicy overflowPolicy, boolean singleConsumer) {
        this.queue = queue;
        if (!singleConsumer || queue instanceof CircularBuffer)
            this.sync = new SimpleConditionSynchronizer(this);
        else
            this.sync = new OwnedSynchronizer(this);

        this.overflowPolicy = overflowPolicy;
        this.sendersSync = overflowPolicy == OverflowPolicy.BLOCK ? new SimpleConditionSynchronizer(this) : null;
    }
View Full Code Here

     */
    public final void set(V value) {
        if (sync == null)
            throw new IllegalStateException("Value has already been set (and can only be set once)");
        this.value = value;
        final SimpleConditionSynchronizer s = sync;
        sync = null; // must be done before signal
        s.signalAll();
    }
View Full Code Here

     */
    @Override
    @Suspendable
    public V get() throws InterruptedException {
        try {
            final SimpleConditionSynchronizer s = sync;
            if (s != null) {
                Object token = s.register();
                try {
                    for (int i = 0; sync != null; i++)
                        s.await(i);
                } finally {
                    s.unregister(token);
                }
            }
            return value;
        } catch (SuspendExecution e) {
            throw new AssertionError(e);
View Full Code Here

TOP

Related Classes of co.paralleluniverse.strands.SimpleConditionSynchronizer

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.