For IntelliJ IDEA,
Android Studio or Eclipse



private void myMethod () {CountDownLatch c =
new CountDownLatch(count)
List list;new CountDownLatch(list.size())
AtomicReference atomicReference;atomicReference.get()
- Smart code suggestions by Codota
}
@Override public void run() { try { okayToRun.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } runCalled.getAndIncrement(); } },
@GwtIncompatible // threads public void testSubmitAsync_asyncCallable_cancelledBeforeApplyingFunction() throws InterruptedException { final AtomicBoolean callableCalled = new AtomicBoolean(); AsyncCallable<Integer> callable = new AsyncCallable<Integer>() { @Override public ListenableFuture<Integer> call() { callableCalled.set(true); return immediateFuture(1); } }; ExecutorService executor = newSingleThreadExecutor(); // Pause the executor. final CountDownLatch beforeFunction = new CountDownLatch(1); executor.execute( new Runnable() { @Override public void run() { awaitUninterruptibly(beforeFunction); } }); ListenableFuture<Integer> future = submitAsync(callable, executor); future.cancel(false); // Unpause the executor. beforeFunction.countDown(); executor.shutdown(); assertTrue(executor.awaitTermination(5, SECONDS)); assertFalse(callableCalled.get()); }
@Test public void testServerConnectionHandler() throws Exception { AtomicInteger status = new AtomicInteger(); AtomicReference<HttpConnection> connRef = new AtomicReference<>(); server.connectionHandler(conn -> { assertEquals(0, status.getAndIncrement()); assertNull(connRef.getAndSet(conn)); }); server.requestHandler(req -> { assertEquals(1, status.getAndIncrement()); assertSame(connRef.get(), req.connection()); req.response().end(); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { testComplete(); }); await(); }
@Override public String load(String key) throws InterruptedException { callCount.incrementAndGet(); startSignal.await(); throw e; } });
@Test public void testListenableFuture() throws Exception { final AtomicInteger statusCode = new AtomicInteger(500); try (AsyncHttpClient ahc = asyncHttpClient()) { final CountDownLatch latch = new CountDownLatch(1); final ListenableFuture<Response> future = ahc.prepareGet(getTargetUrl()).execute(); future.addListener(() -> { try { statusCode.set(future.get().getStatusCode()); latch.countDown(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } }, Executors.newFixedThreadPool(1)); latch.await(10, TimeUnit.SECONDS); assertEquals(statusCode.get(), 200); } }
@Test(timeout = 1000L) public void get_memoizes() throws InterruptedException { int getCount = 1000; AtomicInteger value = new AtomicInteger(); Lazy<Integer> lazyInt = new Lazy<Integer>() { final AtomicInteger val = new AtomicInteger(); @Override protected Integer compute() { return val.incrementAndGet(); } }; CountDownLatch latch = new CountDownLatch(getCount); Executor exec = Executors.newFixedThreadPool(10); for (int i = 0; i < getCount; i++) { exec.execute(() -> { // if lazy computes multiple times, the result of lazyInt.get() > 1 value.getAndAdd(lazyInt.get()); latch.countDown(); }); } latch.await(); assertThat(value.get()).isEqualTo(getCount); } }
@Test public void tryToReproduceTheIssue() throws Exception { // GIVEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); CountDownLatch startSignal = new CountDownLatch( 1 ); AtomicBoolean stopSignal = new AtomicBoolean(); AtomicReference<Exception> failure = new AtomicReference<>(); Node parentNode = createNode( db ); Collection<Worker> workers = createWorkers( db, startSignal, stopSignal, failure, parentNode ); // WHEN startSignal.countDown(); sleep( 500 ); stopSignal.set( true ); awaitWorkersToEnd( workers ); // THEN if ( failure.get() != null ) { throw new Exception( "A worker failed", failure.get() ); } }
@Override public void run() { cache.getUnchecked(s); computedCount.incrementAndGet(); tasksFinished.countDown(); } });
AtomicBoolean computationShouldWait = new AtomicBoolean(); CountDownLatch computationLatch = new CountDownLatch(1); QueuingRemovalListener<String, String> listener = queuingRemovalListener(); final LoadingCache<String, String> cache = expectedKeys.add(s); computationShouldWait.set(true); final AtomicInteger computedCount = new AtomicInteger(); ExecutorService threadPool = Executors.newFixedThreadPool(nThreads); final CountDownLatch tasksFinished = new CountDownLatch(nTasks); for (int i = 0; i < nTasks; i++) { final String s = "a" + i; @SuppressWarnings("unused") // go/futurereturn-lsc Future<?> possiblyIgnoredError = threadPool.submit( new Runnable() { @Override computationLatch.countDown(); while (computedCount.get() < nThreads) { Thread.yield(); tasksFinished.await();
@Test public void testHttpClientConnectionCloseAfterRequestEnd() throws Exception { CountDownLatch started = new CountDownLatch(1); client = vertx.createHttpClient(); AtomicReference<EndpointMetric> endpointMetrics = new AtomicReference<>(); server = vertx.createHttpServer().requestHandler(req -> { endpointMetrics.set(((FakeHttpClientMetrics)FakeHttpClientMetrics.getMetrics(client)).endpoint("localhost:8080")); req.response().end(); }).listen(8080, "localhost", ar -> { assertTrue(ar.succeeded()); started.countDown(); }); awaitLatch(started); CountDownLatch closed = new CountDownLatch(1); HttpClientRequest req = client.get(8080, "localhost", "/somepath"); req.handler(resp -> { HttpConnection conn = req.connection(); conn.closeHandler(v2 -> { closed.countDown(); }); conn.close(); req.end(); awaitLatch(closed); EndpointMetric val = endpointMetrics.get(); assertWaitUntil(() -> val.connectionCount.get() == 0); assertEquals(0, val.queueSize.get()); assertEquals(0, val.requests.get());
@Override public Integer call() throws Exception { enterLatch.countDown(); try { new CountDownLatch(1).await(); // wait forever throw new AssertionError(); } catch (InterruptedException e) { interruptedExceptionThrown.set(true); throw e; } finally { } } });
startLatch.await(); stopLatch.countDown(); return; final float indexedVal = indexed2.get(j); if (Floats.compare(val, indexedVal) != 0) { failureHappened.set(true); reason.set(StringUtils.format("Thread2[%d]: %f != %f", j, val, indexedVal)); stopLatch.countDown(); return; reason.set(e.getMessage()); failureHappened.set(true); stopLatch.countDown();
countMap.put(msg.body().getString("deploymentID"), msg.body().getInteger("count")); }); CountDownLatch latch = new CountDownLatch(1); AtomicReference<String> deploymentID1 = new AtomicReference<>(); AtomicReference<String> deploymentID2 = new AtomicReference<>(); setIsolationGroup(group1).setIsolatedClasses(isolatedClasses), ar -> { assertTrue(ar.succeeded()); deploymentID1.set(ar.result()); assertEquals(0, TestVerticle.instanceCount.get()); vertx.deployVerticle(verticleID, new DeploymentOptions().setIsolationGroup(group2).setIsolatedClasses(isolatedClasses), ar2 -> { assertTrue(ar2.succeeded()); deploymentID2.set(ar2.result()); assertEquals(0, TestVerticle.instanceCount.get()); latch.countDown(); }); }); assertEquals(count1, countMap.get(deploymentID1.get()).intValue()); assertEquals(count2, countMap.get(deploymentID2.get()).intValue()); assertTrue(expectedSuccess);
ExecutorService exec = Executors.newFixedThreadPool(1); try { server.connectHandler(so -> { so.handler(buff -> { assertEquals(256, buff.length()); CountDownLatch latch = new CountDownLatch(1); exec.execute(() -> { latch.countDown(); so.write(expected); }); }); startServer(); AtomicInteger done = new AtomicInteger(); for (int i = 0;i < num;i++) { client.connect(testAddress, ar -> { assertEquals(expected, buff); so.close(); int val = done.incrementAndGet(); if (val == num) { testComplete(); exec.shutdown();