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:it.stilo.g.util.WeightedZIPFianGenerator.java

public static void generate(WeightedDirectedGraph g, double exponent, int degree, int runner) {

    long time = System.currentTimeMillis();
    final CountDownLatch latch = new CountDownLatch(runner);

    ZipfDistribution dist = new ZipfDistribution(g.size, exponent);
    Thread[] workers = new Thread[runner];
    for (int i = 0; i < runner; i++) {
        workers[i] = new Thread(new WeightedZIPFianGenerator(g, latch, dist, degree, i, runner));
        workers[i].setName("" + i);
        workers[i].start();//w  ww.java 2  s .  c  o m
    }

    try {
        latch.await();
    } catch (InterruptedException e) {
        logger.debug(e);
    }

    logger.info(WeightedZIPFianGenerator.class.getName() + "\t" + ((System.currentTimeMillis() - time) / 1000d)
            + "s");
}

From source file:ch.cyberduck.core.http.DelayedHttpEntity.java

public DelayedHttpEntity() {
    this(new CountDownLatch(1));
}

From source file:com.liferay.mobile.android.CookieAuthenticationTest.java

@Test
public void signIn() throws Exception {
    Session session = new SessionImpl(this.session);
    final JSONArray[] sites = { null };
    final CountDownLatch lock = new CountDownLatch(1);

    CookieSignIn.signIn(session, new CookieSignIn.CookieCallback() {

        @Override/*w  w w  . j a  v a 2s.co m*/
        public void onSuccess(Session session) {
            try {
                GroupService service = new GroupService(session);
                sites[0] = service.getUserSitesGroups();
                lock.countDown();
            } catch (Exception e) {
                onFailure(e);
            }
        }

        @Override
        public void onFailure(Exception exception) {
            fail(exception.getMessage());
            lock.countDown();
        }

    });

    lock.await(2000, TimeUnit.MILLISECONDS);
    GroupServiceTest.assertUserSites(sites[0]);
}

From source file:com.frostwire.search.VuzeMagnetDownloader.java

public byte[] download(String magnet, int timeout) {

    CountDownLatch signal = new CountDownLatch(1);

    String saveDir = SharingSettings.TORRENTS_DIR_SETTING.getValue().getAbsolutePath();

    TorrentDownloaderListener listener = new TorrentDownloaderListener(signal);

    TorrentDownloader td = TorrentDownloaderFactory.create(listener, magnet, null, saveDir);

    td.start();//  w w w  . j  a va  2s  .  c o  m

    try {
        signal.await(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        // ignore
    }

    if (listener.getData() == null) {
        td.cancel();
    }

    return listener.getData();
}

From source file:io.dropwizard.websockets.DropWizardWebsocketsTest.java

@BeforeClass
public static void setUpClass() throws InterruptedException, IOException {
    CountDownLatch serverStarted = new CountDownLatch(1);
    Thread serverThread = new Thread(GeneralUtils.rethrow(() -> new MyApp(serverStarted)
            .run(new String[] { "server", Resources.getResource("server.yml").getPath() })));
    serverThread.setDaemon(true);/*  w  w  w. j av  a 2  s  .  com*/
    serverThread.start();
    serverStarted.await(10, SECONDS);
}

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  w ww .j ava2  s.co  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:com.alibaba.druid.DBCPTest.java

public void test_max() throws Exception {
    Class.forName("com.alibaba.druid.mock.MockDriver");

    final BasicDataSource dataSource = new BasicDataSource();
    //        final DruidDataSource dataSource = new DruidDataSource();
    dataSource.setInitialSize(3);/*from   w  ww  .j a v a2s  .c om*/
    dataSource.setMaxActive(20);
    dataSource.setMaxIdle(20);
    dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver");
    dataSource.setUrl("jdbc:mock:xxx");

    final int THREAD_COUNT = 200;
    final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT);
    final CountDownLatch startLatch = new CountDownLatch(1);
    Thread[] threads = new Thread[THREAD_COUNT];
    for (int i = 0; i < THREAD_COUNT; ++i) {
        threads[i] = new Thread() {

            public void run() {
                try {
                    startLatch.await();
                    for (int i = 0; i < 1000; ++i) {
                        Connection conn = dataSource.getConnection();
                        Thread.sleep(1);
                        conn.close();
                    }
                } catch (Exception e) {
                } finally {
                    endLatch.countDown();
                }
            }
        };
        threads[i].start();
    }

    startLatch.countDown();

    endLatch.await();

    //        System.out.println(dataSource.getNumIdle());
    System.out.println(MockDriver.instance.getConnections().size());
    System.out.println(MockDriver.instance.getConnectionCloseCount());
}

From source file:org.dataconservancy.packaging.gui.BaseGuiTest.java

/**
 * Wait until a JavaFX application is launched.
 * /*from ww w  .j a  va 2s.co m*/
 * TODO: Use the regular app class and modify it to expose various properties to tests?
 * @throws InterruptedException
 */

@BeforeClass
public static void setupJavaFX() throws InterruptedException {
    if (javafx_setup_latch != null) {
        return;
    }

    javafx_setup_latch = new CountDownLatch(1);

    Runnable init = () -> Application.launch(SkeletonApp.class);

    System.out.println("JavaFx initialising...");
    long timeMillis = System.currentTimeMillis();

    new Thread(init).start();

    javafx_setup_latch.await();

    System.out.println("JavaFx is initialised in " + (System.currentTimeMillis() - timeMillis) + "ms");
}

From source file:com.skype.connector.linux.SkypeFramework.java

static void init() throws LoadLibraryException {
    synchronized (initializedFieldMutex) {
        if (!initialized) {
            if (SystemUtils.OS_ARCH.contains("64")) {
                ConnectorUtils.loadLibrary(getLibName("x64"));
            } else {
                ConnectorUtils.loadLibrary(getLibName("x86"));
            }/*from  ww w.  j  a  v a2  s.co  m*/
            setup0();

            eventLoopFinishedLatch = new CountDownLatch(1);
            eventLoop = new Thread(new Runnable() {
                public void run() {
                    runEventLoop0();
                    eventLoopFinishedLatch.countDown();
                }
            }, "Skype4Java Event Loop");
            eventLoop.setDaemon(true);
            eventLoop.start();
            initialized = true;
        }
    }
}

From source file:jcuda.jcublas.kernel.TestMultipleThreads.java

@Test
public void testMultipleThreads() throws InterruptedException {
    int numThreads = 10;
    final INDArray array = Nd4j.rand(3000, 3000);
    final INDArray expected = array.dup().mmul(array).mmul(array).div(array).div(array);
    final AtomicInteger correct = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(numThreads);

    ExecutorService executors = Executors.newCachedThreadPool();

    for (int x = 0; x < numThreads; x++) {
        executors.execute(new Runnable() {
            @Override/*from  www  .j  a  va  2s  . c  om*/
            public void run() {
                try {
                    int total = 10;
                    int right = 0;
                    for (int x = 0; x < total; x++) {
                        StopWatch watch = new StopWatch();
                        watch.start();
                        INDArray actual = array.dup().mmul(array).mmul(array).div(array).div(array);
                        watch.stop();
                        System.out.println("MMUL took " + watch.getTime());
                        if (expected.equals(actual))
                            right++;
                    }

                    if (total == right)
                        correct.incrementAndGet();
                } finally {
                    latch.countDown();
                }

            }
        });
    }

    latch.await();

    assertEquals(numThreads, correct.get());

}