Examples of AtomicReference


Examples of java.util.concurrent.atomic.AtomicReference

    @Test
    public void testEncoder() throws IOException, ServletException, InterruptedException {

        AtmosphereRequest request = new AtmosphereRequest.Builder().pathInfo("/h").method("GET").build();
        AtmosphereResponse response = AtmosphereResponse.newInstance();
        final AtomicReference<String> ref = new AtomicReference();

        response.asyncIOWriter(new AsyncIOWriterAdapter() {
            @Override
            public AsyncIOWriter write(AtmosphereResponse r, byte[] data) throws IOException {
                ref.set(new String(data));
                return this;
            }
        });
        framework.doCometSupport(request, response);
        assertNotNull(r.get());
        latch.get().await(5, TimeUnit.SECONDS);
        r.get().resume();
        assertNotNull(message.get());
        assertEquals(message.get(), "message");
        assertEquals(ref.get(), "message-yo!");

    }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicReference

      }), taskOwner);
    }

    @SuppressWarnings("unchecked")
    @Test public void testIteratorRetainAcrossTransactions() throws Exception {
  final AtomicReference<Iterator<Object>> iterRef = new AtomicReference();
  txnScheduler.runTask(
      new TestTask(new TestAbstractKernelRunnable() {
    public void run() {
        set.add(one);
        Iterator<Object> iter = set.iterator();
        iterRef.set(iter);
        dataService.setBinding("iter",
             new ManagedSerializable(iter));
    }
      }), taskOwner);
  txnScheduler.runTask(
      new TestTask(new TestAbstractKernelRunnable() {
    public void run() {
        Iterator<Object> iter = iterRef.get();
        try {
      iter.hasNext();
      fail("Expected TransactionNotActiveException");
        } catch (TransactionNotActiveException e) {
        }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicReference

            throw new BindTransportException("Failed to resolve host [" + bindHost + "]", e);
        }
        final InetAddress hostAddress = hostAddressX;

        PortsRange portsRange = new PortsRange(port);
        final AtomicReference<Exception> lastException = new AtomicReference();
        boolean success = portsRange.iterate(new PortsRange.PortCallback() {
            @Override
            public boolean onPortNumber(int portNumber) {
                try {
                    serverChannel = serverBootstrap.bind(new InetSocketAddress(hostAddress, portNumber));
                } catch (Exception e) {
                    lastException.set(e);
                    return false;
                }
                return true;
            }
        });
        if (!success) {
            throw new BindTransportException("Failed to bind to [" + port + "]", lastException.get());
        }

        logger.debug("Bound to address [{}]", serverChannel.getLocalAddress());

        InetSocketAddress boundAddress = (InetSocketAddress) serverChannel.getLocalAddress();
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicReference

                        + "is not on-site and not permitted by default. Check 'websocket.onsiteonly' setting in configuration.");
            }
        }

        PortsRange portsRange = new PortsRange(port);
        final AtomicReference<Exception> lastException = new AtomicReference();
        boolean success = portsRange.iterate(new PortsRange.PortCallback() {
            @Override
            public boolean onPortNumber(int portNumber) {
                try {
                    serverChannel = serverBootstrap.bind(new InetSocketAddress(hostAddress, portNumber));
                } catch (Exception e) {
                    lastException.set(e);
                    return false;
                }
                return true;
            }
        });
        if (!success) {
            throw new BindHttpException("Failed to bind to [" + port + "]", lastException.get());
        }
        InetSocketAddress boundAddress = (InetSocketAddress) serverChannel.getLocalAddress();
        InetSocketAddress publishAddress;
        try {
            publishAddress = new InetSocketAddress(networkService.resolvePublishHostAddress(publishHost), boundAddress.getPort());
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicReference

    boolean alive() {
        return thread != null && thread.isAlive() && !data.queue.isShutdown();
    }
   
    static RubyThread createThread(final Ruby runtime, final FiberData data, final SizedQueue queue, final Block block) {
        final AtomicReference<RubyThread> fiberThread = new AtomicReference();
        runtime.getFiberExecutor().execute(new Runnable() {
            public void run() {
                ThreadContext context = runtime.getCurrentContext();
                context.setFiber(data.fiber.get());
                context.setRootThread(data.parent);
                fiberThread.set(context.getThread());
               
                IRubyObject init = data.queue.pop(context);

                try {
                    try {
                        IRubyObject result;

                        if (init == NEVER) {
                            result = block.yieldSpecific(context);
                        } else {
                            result = block.yieldArray(context, init, null);
                        }

                        data.prev.data.queue.push(context, new IRubyObject[] { result });
                    } finally {
                        data.queue.shutdown();
                        runtime.getThreadService().disposeCurrentThread();
                    }
                } catch (JumpException.FlowControlException fce) {
                    if (data.prev != null) {
                        data.prev.thread.raise(fce.buildException(runtime).getException());
                    }
                } catch (IRBreakJump bj) {
                    // This is one of the rare cases where IR flow-control jumps
                    // leaks into the runtime impl.
                    if (data.prev != null) {
                        data.prev.thread.raise(((RaiseException)IRException.BREAK_LocalJumpError.getException(runtime)).getException());
                    }
                } catch (IRReturnJump rj) {
                    // This is one of the rare cases where IR flow-control jumps
                    // leaks into the runtime impl.
                    if (data.prev != null) {
                        data.prev.thread.raise(((RaiseException)IRException.RETURN_LocalJumpError.getException(runtime)).getException());
                    }
                } catch (RaiseException re) {
                    if (data.prev != null) {
                        data.prev.thread.raise(re.getException());
                    }
                } catch (Throwable t) {
                    if (data.prev != null) {
                        data.prev.thread.raise(JavaUtil.convertJavaToUsableRubyObject(runtime, t));
                    }
                } finally {
                    // clear reference to the fiber's thread
                    ThreadFiber tf = data.fiber.get();
                    if (tf != null) tf.thread = null;
                }
            }
        });
       
        while (fiberThread.get() == null) {Thread.yield();}
       
        return fiberThread.get();
    }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicReference

    @SuppressWarnings("rawtypes")
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        Object item;
        if (object instanceof AtomicReference) {
            AtomicReference val = (AtomicReference) object;
            item = val.get();
        } else {
            item = ((Reference) object).get();
        }
        serializer.write(item);
    }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicReference

        Object itemObject = parser.parseObject(itemType);

        Type rawType = paramType.getRawType();
        if (rawType == AtomicReference.class) {
            return (T) new AtomicReference(itemObject);
        }

        if (rawType == WeakReference.class) {
            return (T) new WeakReference(itemObject);
        }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicReference

        }
    }

    @Test
    public void testIsolationInConcurrentReadWriteTransactions() throws Exception {
        final AtomicReference error = new AtomicReference();
        final CountDownLatch mutationLatch = new CountDownLatch(1);
        final CountDownLatch preCommitLatch = new CountDownLatch(1);
        final CountDownLatch commitLatch = new CountDownLatch(2);
        ExecutorService executor = Executors.newCachedThreadPool();
        //
        executor.submit(new Runnable() {

            public void run() {
                try {
                    Transaction writer = pageFile.tx();
                    Index<String, Long> index = openIndex(writer);
                    try {
                        for (int i = 0; i < 1000; i++) {
                            index.put("" + i, Long.valueOf(i));
                        }
                        mutationLatch.countDown();
                        if (preCommitLatch.await(60, TimeUnit.SECONDS)) {
                            writer.commit();
                        } else {
                            throw new RuntimeException();
                        }
                    } catch (InterruptedException ex) {
                        error.set(ex);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    error.compareAndSet(null, ex);
                } finally {
                    commitLatch.countDown();
                }
            }

        });
        //
        executor.submit(new Runnable() {

            public void run() {
                try {
                    Transaction reader = pageFile.tx();
                    Index<String, Long> index = openIndex(reader);
                    try {
                        if (mutationLatch.await(60, TimeUnit.SECONDS)) {
                            for (int i = 0; i < 1000; i++) {
                                if (index.get("" + i) != null) {
                                    error.set(new RuntimeException("Bad transaction isolation!"));
                                    throw new RuntimeException();
                                }
                            }
                            reader.commit();
                            preCommitLatch.countDown();
                        } else {
                            throw new RuntimeException();
                        }
                    } catch (InterruptedException ex) {
                        error.set(ex);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    error.compareAndSet(null, ex);
                } finally {
                    commitLatch.countDown();
                }
            }

        });
        assertTrue(commitLatch.await(60, TimeUnit.SECONDS));
        if (error.get() == null) {
            Transaction checker = pageFile.tx();
            Index<String, Long> index = openIndex(checker);
            for (int i = 0; i < 1000; i++) {
                assertEquals(Long.valueOf(i), index.get("" + i));
            }
            checker.commit();
        } else {
            throw (Exception) error.get();
        }
        //
        executor.shutdownNow();
    }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicReference

        executor.shutdownNow();
    }

    @Test
    public void testConflictResolutionInConcurrentWriteTransactions() throws Exception {
        final AtomicReference error = new AtomicReference();
        final CountDownLatch preCommitLatch = new CountDownLatch(2);
        final CountDownLatch commitLatch = new CountDownLatch(2);
        ExecutorService executor = Executors.newCachedThreadPool();
        //
        executor.submit(new Runnable() {

            public void run() {
                try {
                    Transaction writer = pageFile.tx();
                    Index<String, Long> index = openIndex(writer);
                    try {
                        for (int i = 0; i < 1000; i++) {
                            index.put("" + i, Long.valueOf(i));
                        }
                        preCommitLatch.countDown();
                        if (preCommitLatch.await(10, TimeUnit.SECONDS)) {
                            try {
                                writer.commit();
                                System.out.println("Committed from 1.");
                            } catch (OptimisticUpdateException ex) {
                                System.out.println("Replaying from 1...");
                                run();
                            }
                        } else {
                            throw new RuntimeException();
                        }
                    } catch (InterruptedException ex) {
                        error.set(ex);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    error.compareAndSet(null, ex);
                } finally {
                    commitLatch.countDown();
                }
            }

        });
        //
        executor.submit(new Runnable() {

            public void run() {
                try {
                    Transaction writer = pageFile.tx();
                    Index<String, Long> index = openIndex(writer);
                    try {
                        for (int i = 1000; i < 2000; i++) {
                            index.put("" + i, Long.valueOf(i));
                        }
                        preCommitLatch.countDown();
                        if (preCommitLatch.await(10, TimeUnit.SECONDS)) {
                            try {
                                writer.commit();
                                System.out.println("Committed from 2.");
                            } catch (OptimisticUpdateException ex) {
                                System.out.println("Replaying from 2...");
                                run();
                            }
                        } else {
                            throw new RuntimeException();
                        }
                    } catch (InterruptedException ex) {
                        error.set(ex);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    error.compareAndSet(null, ex);
                } finally {
                    commitLatch.countDown();
                }
            }

        });
        assertTrue(commitLatch.await(60, TimeUnit.SECONDS));
        if (error.get() == null) {
            Transaction checker = pageFile.tx();
            Index<String, Long> index = openIndex(checker);
            for (int i = 0; i < 2000; i++) {
                assertEquals(Long.valueOf(i), index.get("" + i));
            }
            checker.commit();
        } else {
            throw (Exception) error.get();
        }
        //
        executor.shutdownNow();
    }
View Full Code Here

Examples of java.util.concurrent.atomic.AtomicReference

final public class Atom extends ARef implements IAtom{
final AtomicReference state;

public Atom(Object state){
  this.state = new AtomicReference(state);
}
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.