Package java.util.concurrent.atomic

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


        }
    }

    @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

        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

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

public Atom(Object state){
  this.state = new AtomicReference(state);
}
View Full Code Here

  this.state = new AtomicReference(state);
}

public Atom(Object state, IPersistentMap meta){
  super(meta);
  this.state = new AtomicReference(state);
}
View Full Code Here

    //  String hostname = null;
    private AtomicReference hostname;
    // Private constructor prevents instantiation from other classes
    private FormatGetHostName() {
        hostname = new AtomicReference();
        // Set IP to something, then invoke a thread to get the hostname itself, if the
        // thread takes time to complete (DNS is slow), the IP address will be used until
        // the thread completes (then the "hostname" should be used).
        hostname.set(getIp());
        Thread lThread = (new Thread("loggerFormatGetHostName") {
View Full Code Here

                        throw new RuntimeException(e);
                    }
                }
            });

            final AtomicReference reference = new AtomicReference();
            final ICompletableFuture completableFuture = es.asCompletableFuture(future);
            completableFuture.andThen(new ExecutionCallback() {
                @Override
                public void onResponse(Object response) {
                    reference.set(response);
                    latch2.countDown();
                }

                @Override
                public void onFailure(Throwable t) {
                    reference.set(t);
                    latch2.countDown();
                }
            });

            latch1.countDown();
            latch2.await(30, TimeUnit.SECONDS);
            assertEquals("success", reference.get());

        } finally {
            executorService.shutdown();
        }
    }
View Full Code Here

                        throw new RuntimeException(e);
                    }
                }
            });

            final AtomicReference reference1 = new AtomicReference();
            final AtomicReference reference2 = new AtomicReference();
            final ICompletableFuture completableFuture = es.asCompletableFuture(future);
            completableFuture.andThen(new ExecutionCallback() {
                @Override
                public void onResponse(Object response) {
                    reference1.set(response);
                    latch2.countDown();
                }

                @Override
                public void onFailure(Throwable t) {
                    reference1.set(t);
                    latch2.countDown();
                }
            });
            completableFuture.andThen(new ExecutionCallback() {
                @Override
                public void onResponse(Object response) {
                    reference2.set(response);
                    latch2.countDown();
                }

                @Override
                public void onFailure(Throwable t) {
                    reference2.set(t);
                    latch2.countDown();
                }
            });

            latch1.countDown();
            latch2.await(30, TimeUnit.SECONDS);
            assertEquals("success", reference1.get());
            assertEquals("success", reference2.get());

        } finally {
            executorService.shutdown();
        }
    }
View Full Code Here

            });

            final ICompletableFuture completableFuture = es.asCompletableFuture(future);
            latch1.await(30, TimeUnit.SECONDS);

            final AtomicReference reference = new AtomicReference();
            completableFuture.andThen(new ExecutionCallback() {
                @Override
                public void onResponse(Object response) {
                    reference.set(response);
                    latch2.countDown();
                }

                @Override
                public void onFailure(Throwable t) {
                    reference.set(t);
                    latch2.countDown();
                }
            });

            latch2.await(30, TimeUnit.SECONDS);
            if (reference.get() instanceof Throwable) {
                ((Throwable) reference.get()).printStackTrace();
            }

            assertEquals("success", reference.get());

        } finally {
            executorService.shutdown();
        }
    }
View Full Code Here

                }
            });

            latch1.await(30, TimeUnit.SECONDS);

            final AtomicReference reference1 = new AtomicReference();
            final AtomicReference reference2 = new AtomicReference();
            final ICompletableFuture completableFuture = es.asCompletableFuture(future);
            completableFuture.andThen(new ExecutionCallback() {
                @Override
                public void onResponse(Object response) {
                    reference1.set(response);
                    latch2.countDown();
                }

                @Override
                public void onFailure(Throwable t) {
                    reference1.set(t);
                    latch2.countDown();
                }
            });
            completableFuture.andThen(new ExecutionCallback() {
                @Override
                public void onResponse(Object response) {
                    reference2.set(response);
                    latch2.countDown();
                }

                @Override
                public void onFailure(Throwable t) {
                    reference2.set(t);
                    latch2.countDown();
                }
            });

            latch2.await(30, TimeUnit.SECONDS);
            assertEquals("success", reference1.get());
            assertEquals("success", reference2.get());

        } finally {
            executorService.shutdown();
        }
    }
View Full Code Here

TOP

Related Classes of java.util.concurrent.atomic.AtomicReference

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.