Example usage for java.util.concurrent CountDownLatch CountDownLatch

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

Introduction

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

Prototype

public CountDownLatch(int count) 

Source Link

Document

Constructs a CountDownLatch initialized with the given count.

Usage

From source file:Main.java

public static void runOnMainSync(final @NonNull Runnable runnable) {
    if (isMainThread()) {
        runnable.run();//from w  w  w .  ja  v a2 s . c  o m
    } else {
        final CountDownLatch sync = new CountDownLatch(1);
        runOnMain(new Runnable() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {
                    sync.countDown();
                }
            }
        });
        try {
            sync.await();
        } catch (InterruptedException ie) {
            throw new AssertionError(ie);
        }
    }
}

From source file:com.newlandframework.test.RpcParallelTest.java

public static void parallelAddCalcTask(AddCalculate calc, int parallel) throws InterruptedException {
    ///*from  ww  w.  ja  va2 s  .  c  o  m*/
    StopWatch sw = new StopWatch();
    sw.start();

    CountDownLatch signal = new CountDownLatch(1);
    CountDownLatch finish = new CountDownLatch(parallel);

    for (int index = 0; index < parallel; index++) {
        AddCalcParallelRequestThread client = new AddCalcParallelRequestThread(calc, signal, finish, index);
        new Thread(client).start();
    }

    signal.countDown();
    finish.await();
    sw.stop();

    String tip = String.format("RPC: [%s] ", sw.getTime());
    System.out.println(tip);
}

From source file:me.smoe.adar.zookeeper.zookeeper.Zookeeper.java

public static ZooKeeper getClient(String scheme, byte[] auth) throws Exception {
    CountDownLatch latch = new CountDownLatch(1);

    ZooKeeper zooKeeper = new ZooKeeper(CONNECT, 5000, (e) -> {
        latch.countDown();//w  w  w  .  j  a  v a  2 s  . com
    });

    latch.await();

    if (StringUtils.isNotEmpty(scheme) && auth != null) {
        zooKeeper.addAuthInfo(scheme, auth);
    }

    return zooKeeper;
}

From source file:com.frostwire.android.MediaScanner.java

private static void scanFiles(final Context context, List<String> paths, int retries) {
    if (paths.size() == 0) {
        return;/*from w ww.j a  va 2s . co  m*/
    }

    LOG.info("About to scan files n: " + paths.size() + ", retries: " + retries);

    final LinkedList<String> failedPaths = new LinkedList<>();

    final CountDownLatch finishSignal = new CountDownLatch(paths.size());

    MediaScannerConnection.scanFile(context, paths.toArray(new String[0]), null, (path, uri) -> {
        try {
            boolean success = true;
            if (uri == null) {
                success = false;
                failedPaths.add(path);
            } else {
                // verify the stored size four faulty scan
                long size = getSize(context, uri);
                if (size == 0) {
                    LOG.warn("Scan returned an uri but stored size is 0, path: " + path + ", uri:" + uri);
                    success = false;
                    failedPaths.add(path);
                }
            }
            if (!success) {
                LOG.info("Scan failed for path: " + path + ", uri: " + uri);
            }
        } finally {
            finishSignal.countDown();
        }
    });

    try {
        finishSignal.await(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        // ignore
    }

    if (failedPaths.size() > 0 && retries > 0) {
        // didn't want to do this, but there is a serious timing issue with the SD
        // and storage in general
        SystemClock.sleep(2000);
        scanFiles(context, failedPaths, retries - 1);
    }
}

From source file:com.manning.siia.pipeline.Counter.java

public synchronized void resetLatch(int latchSize) {
    latch = new CountDownLatch(latchSize);
}

From source file:com.clicktravel.cheddar.server.flow.control.RestAdapterStartLatchConfiguration.java

@Bean
public CountDownLatch restAdapterStartLatch() {
    return new CountDownLatch(1); // start latch is initially blocked
}

From source file:com.microsoft.office.core.EventsAsyncTestCase.java

@BeforeClass
public static void retrieveCalendar() throws Exception {
    final ICalendars cals = Me.getCalendars();
    final CountDownLatch cdl = new CountDownLatch(1);
    // an empty iterator will be returned for any entity set unless you call fetch()
    Futures.addCallback(cals.fetchAsync(), new FutureCallback<Void>() {
        @Override//from w  w w.j  a  va  2 s . co m
        public void onFailure(Throwable t) {
            cdl.countDown();
        }

        @Override
        public void onSuccess(Void result) {
            Iterator<ICalendar> iterator = cals.iterator();
            if (iterator.hasNext()) {
                calendar = iterator.next();
            }
            cdl.countDown();
        }
    });

    cdl.await(60000, TimeUnit.MILLISECONDS);
    if (calendar == null) {
        fail("No calendar found");
    }
}

From source file:com.newlandframework.test.RpcParallelTest.java

public static void parallelMultiCalcTask(MultiCalculate calc, int parallel) throws InterruptedException {
    ////from  w  w  w .  jav a  2 s .c o m
    StopWatch sw = new StopWatch();
    sw.start();

    CountDownLatch signal = new CountDownLatch(1);
    CountDownLatch finish = new CountDownLatch(parallel);

    for (int index = 0; index < parallel; index++) {
        MultiCalcParallelRequestThread client = new MultiCalcParallelRequestThread(calc, signal, finish, index);
        new Thread(client).start();
    }

    signal.countDown();
    finish.await();
    sw.stop();

    String tip = String.format("RPC: [%s] ", sw.getTime());
    System.out.println(tip);
}

From source file:com.alibaba.otter.node.etl.load.WeightControllerTest.java

@Test
public void test_simple() {
    int thread = 10;
    int count = 10;
    WeightController controller = new WeightController(thread);
    CountDownLatch latch = new CountDownLatch(thread);
    WeightWorkerTest[] workers = new WeightWorkerTest[thread];
    for (int i = 0; i < thread; i++) {
        int[] weights = new int[count];
        for (int j = 0; j < count; j++) {
            weights[j] = RandomUtils.nextInt(count);
        }//  w  w w .  j a v  a2  s  . com
        workers[i] = new WeightWorkerTest(i, weights, controller, latch);
    }

    for (int i = 0; i < thread; i++) {
        workers[i].start();
    }

    try {
        latch.await();
    } catch (InterruptedException e) {
        want.fail();
    }
}

From source file:com.amazonaws.eclipse.core.accounts.profiles.SdkCredentialsFileContentMonitorTest.java

@Test
public void testFileChangedCallback() throws InterruptedException {

    final CountDownLatch latch = new CountDownLatch(1);

    SdkCredentialsFileContentMonitor monitor = new SdkCredentialsFileContentMonitor(targetFile,
            MONITOR_POLLING_INTERVAL_MILLIS, new FileAlterationListenerAdaptor() {

                @Override//from w ww.j ava  2  s. c  o  m
                public void onFileChange(final File changedFile) {
                    latch.countDown();
                }
            });
    monitor.setDebugMode(true);
    monitor.start();

    touch(targetFile);

    long waitTime = MONITOR_POLLING_INTERVAL_MILLIS * 2;
    Assert.assertTrue("File monitor callback not invoked after waiting for " + waitTime + " ms.",
            latch.await(waitTime, TimeUnit.MILLISECONDS));
}