Example usage for java.util.concurrent CyclicBarrier CyclicBarrier

List of usage examples for java.util.concurrent CyclicBarrier CyclicBarrier

Introduction

In this page you can find the example usage for java.util.concurrent CyclicBarrier CyclicBarrier.

Prototype

public CyclicBarrier(int parties) 

Source Link

Document

Creates a new CyclicBarrier that will trip when the given number of parties (threads) are waiting upon it, and does not perform a predefined action when the barrier is tripped.

Usage

From source file:org.nuxeo.ecm.core.storage.sql.TestSQLBackend.java

protected static void runParallelLocking(Serializable nodeId, Repository repository1, Repository repository2)
        throws Throwable {
    CyclicBarrier barrier = new CyclicBarrier(2);
    CountDownLatch firstReady = new CountDownLatch(1);
    long TIME = 1000; // ms
    LockingJob r1 = new LockingJob(repository1, "t1-", nodeId, TIME, firstReady, barrier);
    LockingJob r2 = new LockingJob(repository2, "t2-", nodeId, TIME, null, barrier);
    Thread t1 = null;/*from  ww  w.j ava2 s  .com*/
    Thread t2 = null;
    try {
        t1 = new Thread(r1, "t1");
        t2 = new Thread(r2, "t2");
        t1.start();
        if (firstReady.await(60, TimeUnit.SECONDS)) {
            t2.start();

            t1.join();
            t1 = null;
            t2.join();
            t2 = null;
            if (r1.throwable != null) {
                throw r1.throwable;
            }
            if (r2.throwable != null) {
                throw r2.throwable;
            }
            int count = r1.count + r2.count;
            log.warn("Parallel locks per second: " + count);
        } // else timed out
    } finally {
        // error condition recovery
        if (t1 != null) {
            t1.interrupt();
        }
        if (t2 != null) {
            t2.interrupt();
        }
    }
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.TestLeafQueue.java

@Test
public void testConcurrentAccess() throws Exception {
    YarnConfiguration conf = new YarnConfiguration();
    RMStorageFactory.setConfiguration(conf);
    YarnAPIStorageFactory.setConfiguration(conf);
    DBUtility.InitializeDB();//  w ww.  j  ava  2  s.c  om
    MockRM rm = new MockRM();
    rm.init(conf);
    rm.start();

    final String queue = "default";
    final String user = "user";
    CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler();
    final LeafQueue defaultQueue = (LeafQueue) cs.getQueue(queue);

    final List<FiCaSchedulerApp> listOfApps = createListOfApps(10000, user, defaultQueue);

    final CyclicBarrier cb = new CyclicBarrier(2);
    final List<ConcurrentModificationException> conException = new ArrayList<ConcurrentModificationException>();

    Thread submitAndRemove = new Thread(new Runnable() {

        @Override
        public void run() {

            for (FiCaSchedulerApp fiCaSchedulerApp : listOfApps) {
                defaultQueue.submitApplicationAttempt(fiCaSchedulerApp, user);
            }
            try {
                cb.await();
            } catch (Exception e) {
                // Ignore
            }
            for (FiCaSchedulerApp fiCaSchedulerApp : listOfApps) {
                defaultQueue.finishApplicationAttempt(fiCaSchedulerApp, queue);
            }
        }
    }, "SubmitAndRemoveApplicationAttempt Thread");

    Thread getAppsInQueue = new Thread(new Runnable() {
        List<ApplicationAttemptId> apps = new ArrayList<ApplicationAttemptId>();

        @Override
        public void run() {
            try {
                try {
                    cb.await();
                } catch (Exception e) {
                    // Ignore
                }
                defaultQueue.collectSchedulerApplications(apps);
            } catch (ConcurrentModificationException e) {
                conException.add(e);
            }
        }

    }, "GetAppsInQueue Thread");

    submitAndRemove.start();
    getAppsInQueue.start();

    submitAndRemove.join();
    getAppsInQueue.join();

    assertTrue("ConcurrentModificationException is thrown", conException.isEmpty());
    rm.stop();

}

From source file:org.nuxeo.ecm.core.storage.sql.TestSQLBackend.java

@Test
public void testParallelPrepareUserReadAcls() throws Throwable {
    Session session = repository.getConnection();
    Node root = session.getRootNode();
    session.addChildNode(root, "foo", null, "TestDoc", false);
    session.save();//from www  .  j  av  a 2s.c  o m
    session.close();

    CyclicBarrier barrier = new CyclicBarrier(2);
    CountDownLatch firstReady = new CountDownLatch(1);
    PrepareUserReadAclsJob r1 = new PrepareUserReadAclsJob(firstReady, barrier);
    PrepareUserReadAclsJob r2 = new PrepareUserReadAclsJob(null, barrier);
    Thread t1 = null;
    Thread t2 = null;
    try {
        t1 = new Thread(r1, "t1");
        t2 = new Thread(r2, "t2");
        t1.start();
        if (firstReady.await(60, TimeUnit.SECONDS)) {
            t2.start();

            t1.join();
            t1 = null;
            t2.join();
            t2 = null;
            if (r1.throwable != null) {
                throw r1.throwable;
            }
            if (r2.throwable != null) {
                throw r2.throwable;
            }
        } // else timed out
    } finally {
        // error condition recovery
        if (t1 != null) {
            t1.interrupt();
        }
        if (t2 != null) {
            t2.interrupt();
        }
    }

    // after both threads have run, check that we don't see
    // duplicate documents
    session = repository.getConnection();
    checkOneDoc(session);
    session.close();

}