Example usage for java.util.concurrent CountDownLatch countDown

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

Introduction

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

Prototype

public void countDown() 

Source Link

Document

Decrements the count of the latch, releasing all waiting threads if the count reaches zero.

Usage

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 . ja  va  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();//w w  w .  ja  va  2s  .  c  o 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// w  w  w.j  ava 2s .  c om
        public void run() {
            activity.getFragmentManager().executePendingTransactions();
            latch.countDown();
        }
    });
    latch.await();
}

From source file:com.kurento.kmf.media.AbstractAsyncBaseTest.java

protected static void releaseMediaObject(final MediaObject mo) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);

    mo.release(new Continuation<Void>() {
        @Override/*from www. ja  v  a 2  s . c  o  m*/
        public void onSuccess(Void result) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable cause) {
            throw new KurentoMediaFrameworkException(cause);
        }
    });
    Assert.assertTrue(latch.await(500, MILLISECONDS));
}

From source file:io.undertow.server.handlers.GracefulShutdownTestCase.java

@BeforeClass
public static void setup() {

    shutdown = Handlers.gracefulShutdown(new HttpHandler() {
        @Override/*from   w  ww  . java2  s  .  c  o  m*/
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            final CountDownLatch countDownLatch = latch2.get();
            final CountDownLatch latch = latch1.get();
            if (latch != null) {
                latch.countDown();
            }
            if (countDownLatch != null) {
                countDownLatch.await();
            }
        }
    });
    DefaultServer.setRootHandler(shutdown);
}

From source file:Main.java

public static void runOnMainSync(final @NonNull Runnable runnable) {
    if (isMainThread()) {
        runnable.run();/* ww w . j a  va2  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:Main.java

public static CountDownLatch execute(int threadCount, final Runnable task) {
    final CountDownLatch startSignal = new CountDownLatch(1);
    final CountDownLatch startedSignal = new CountDownLatch(threadCount);
    final CountDownLatch doneSignal = new CountDownLatch(threadCount);
    for (int i = 0; i < threadCount; i++) {
        Thread t = new Thread() {
            public void run() {
                startedSignal.countDown();
                try {
                    startSignal.await();
                } catch (InterruptedException e) {
                    //ignore
                }/*from   w  w  w .j a  v a2 s  . co  m*/

                try {
                    task.run();
                } finally {
                    doneSignal.countDown();
                }

            }
        };

        t.start();
    }

    try {
        startedSignal.await();
    } catch (InterruptedException e) {
        //ignore
    }
    startSignal.countDown();
    return doneSignal;
}

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

public static void parallelAddCalcTask(AddCalculate calc, int parallel) throws InterruptedException {
    ///*from w w  w .  ja  v a  2s . 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:com.newlandframework.test.RpcParallelTest.java

public static void parallelMultiCalcTask(MultiCalculate calc, int parallel) throws InterruptedException {
    ////from  w  w  w.ja  va2s.co 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:Main.java

public static long time(Executor executor, int concurrency, final Runnable action) throws InterruptedException {
    final CountDownLatch ready = new CountDownLatch(concurrency);
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(concurrency);

    for (int i = 0; i < concurrency; i++) {
        executor.execute(new Runnable() {
            @Override//from  www.  j  a  v a2 s  .  c  om
            public void run() {
                ready.countDown(); // Tell timer we're ready
                try {
                    start.await(); // Wait till peers are ready
                    action.run();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } finally {
                    done.countDown(); // Tell timer we're done
                }
            }
        });
    }

    ready.await(); // Wait for all workers to be ready
    long startNanos = System.nanoTime();
    start.countDown(); // And they're off!
    done.await(); // Wait for all workers to finish
    return System.nanoTime() - startNanos;
}