Example usage for java.util.concurrent CountDownLatch await

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

Introduction

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

Prototype

public void await() throws InterruptedException 

Source Link

Document

Causes the current thread to wait until the latch has counted down to zero, unless the thread is Thread#interrupt interrupted .

Usage

From source file:Main.java

public static void await(CountDownLatch latch) {
    try {/*w  w w .j a  va2  s.  c  o m*/
        latch.await();
    } catch (InterruptedException e) {
        // Meh
    }
}

From source file:Main.java

/**
 * Wait for the latch count down to zero.
 *
 * @param latch the latch to wait for//from  www.  j a va  2s  .  c  om
 * @return true if success, false if interrupted
 */
public static final boolean waitFor(CountDownLatch latch) {
    try {
        latch.await();
        return true;
    } catch (InterruptedException e) {
        return false;
    }
}

From source file:Main.java

public static void await(CountDownLatch latch) {
    try {//  ww w .java2s.  co  m
        latch.await();
    } catch (InterruptedException e) {
        throw new Error(e);
    }
}

From source file:Main.java

public static final boolean doAwait(CountDownLatch cl) {
    try {/*from  w  ww  .ja  va2s.  c o  m*/
        cl.await();
        return true;
    } catch (InterruptedException e) {
        return false;
    }
}

From source file:Main.java

public static boolean await(CountDownLatch cl) {
    try {//from   w w w . ja  v  a  2 s .  c o  m
        cl.await();
        return true;
    } catch (InterruptedException e) {
        return false;
    }
}

From source file:Main.java

/**
 * /*from  w w  w .  jav  a  2 s .c  om*/
 * @param threadCount
 * @param task
 * @return costTime
 * @throws InterruptedException 
 */
public static long executeAndWait(int threadCount, final Runnable task) throws InterruptedException {
    CountDownLatch doneSignal = execute(threadCount, task);
    long startTime = System.currentTimeMillis();
    doneSignal.await();
    return System.currentTimeMillis() - startTime;
}

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();/*from  w  w w .  j  av a  2 s.c o  m*/
    });

    latch.await();

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

    return zooKeeper;
}

From source file:Main.java

public static void runAndWait(Runnable action) {
    if (action == null)
        throw new NullPointerException("action");

    // run synchronously on JavaFX thread
    if (Platform.isFxApplicationThread()) {
        action.run();/*from  w  w  w.j av  a 2s. co  m*/
        return;
    }

    // queue on JavaFX thread and wait for completion
    final CountDownLatch doneLatch = new CountDownLatch(1);
    Platform.runLater(() -> {
        try {
            action.run();
        } finally {
            doneLatch.countDown();
        }
    });

    try {
        doneLatch.await();
    } catch (InterruptedException e) {
        // ignore exception
    }
}

From source file:Main.java

private static void waitForFragmentTransaction(final Activity activity) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    activity.runOnUiThread(new Runnable() {
        @Override//from   w ww  . jav a 2  s  . c om
        public void run() {
            activity.getFragmentManager().executePendingTransactions();
            latch.countDown();
        }
    });
    latch.await();
}

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

public static void parallelAddCalcTask(AddCalculate calc, int parallel) throws InterruptedException {
    ////w ww .  j a v a2s .  c om
    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);
}