List of usage examples for java.util.concurrent CountDownLatch await
public void await() throws InterruptedException
From source file:com.example.grpc.springboot.Cmd.java
@Override public void run(String... args) throws Exception { Channel channel = channelFactory.createChannel("EchoService"); CountDownLatch latch = new CountDownLatch(1); EchoServiceStub stub = EchoServiceGrpc.newStub(channel); for (int i = 0; i < 10; ++i) { stub.echo(Echo.newBuilder().setMessage("Hello" + i).build(), new StreamObserver<Echo>() { @Override/* w ww . j ava2s. com*/ public void onNext(Echo value) { System.out.println(value.getMessage()); } @Override public void onError(Throwable t) { latch.countDown(); } @Override public void onCompleted() { System.out.println("Finished"); latch.countDown(); } }); } latch.await(); }
From source file:org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient.java
@Override public NamedList<Object> request(final SolrRequest request, String collection) throws SolrServerException, IOException { if (!(request instanceof UpdateRequest)) { return client.request(request, collection); }//from w ww.j av a 2s.co m UpdateRequest req = (UpdateRequest) request; // this happens for commit... if (streamDeletes) { if ((req.getDocuments() == null || req.getDocuments().isEmpty()) && (req.getDeleteById() == null || req.getDeleteById().isEmpty()) && (req.getDeleteByIdMap() == null || req.getDeleteByIdMap().isEmpty())) { if (req.getDeleteQuery() == null) { blockUntilFinished(); return client.request(request, collection); } } } else { if ((req.getDocuments() == null || req.getDocuments().isEmpty())) { blockUntilFinished(); return client.request(request, collection); } } SolrParams params = req.getParams(); if (params != null) { // check if it is waiting for the searcher if (params.getBool(UpdateParams.WAIT_SEARCHER, false)) { log.info("blocking for commit/optimize"); blockUntilFinished(); // empty the queue return client.request(request, collection); } } try { CountDownLatch tmpLock = lock; if (tmpLock != null) { tmpLock.await(); } Update update = new Update(req, collection); boolean success = queue.offer(update); for (;;) { synchronized (runners) { // see if queue is half full and we can add more runners // special case: if only using a threadCount of 1 and the queue // is filling up, allow 1 add'l runner to help process the queue if (runners.isEmpty() || (queue.remainingCapacity() < queue.size() && runners.size() < threadCount)) { // We need more runners, so start a new one. addRunner(); } else { // break out of the retry loop if we added the element to the queue // successfully, *and* // while we are still holding the runners lock to prevent race // conditions. if (success) break; } } // Retry to add to the queue w/o the runners lock held (else we risk // temporary deadlock) // This retry could also fail because // 1) existing runners were not able to take off any new elements in the // queue // 2) the queue was filled back up since our last try // If we succeed, the queue may have been completely emptied, and all // runners stopped. // In all cases, we should loop back to the top to see if we need to // start more runners. // if (!success) { success = queue.offer(update, 100, TimeUnit.MILLISECONDS); } } } catch (InterruptedException e) { log.error("interrupted", e); throw new IOException(e.getLocalizedMessage()); } // RETURN A DUMMY result NamedList<Object> dummy = new NamedList<>(); dummy.add("NOTE", "the request is processed in a background stream"); return dummy; }
From source file:com.microsoft.office.integration.test.FoldersAsyncTestCase.java
private void deleteAndCheck() throws Exception { removeFolder();//w w w . jav a 2 s .c o m final CountDownLatch cdl = new CountDownLatch(1); Futures.addCallback(Me.getFolders().getAsync(folder.getId()), new FutureCallback<IFolder>() { public void onFailure(Throwable t) { reportError(t); cdl.countDown(); } public void onSuccess(IFolder result) { try { assertNull(result); } catch (Throwable t) { reportError(t); } cdl.countDown(); } }); cdl.await(); }
From source file:com.microsoft.office.integration.test.FoldersAsyncTestCase.java
private void createAndCheck() throws Exception { prepareFolder();//from w w w.j av a 2 s.c o m final CountDownLatch cdl = new CountDownLatch(1); Futures.addCallback(Me.flushAsync(), new FutureCallback<Void>() { public void onFailure(Throwable t) { reportError(t); cdl.countDown(); } public void onSuccess(Void result) { try { assertTrue(StringUtils.isNotEmpty(folder.getId())); } catch (Throwable t) { reportError(t); } cdl.countDown(); } }); cdl.await(); }
From source file:com.netflix.config.ConcurrentMapConfigurationTest.java
@Test public void testConcurrency() { final ConcurrentMapConfiguration conf = new ConcurrentMapConfiguration(); ExecutorService exectuor = Executors.newFixedThreadPool(20); final CountDownLatch doneSignal = new CountDownLatch(1000); for (int i = 0; i < 1000; i++) { final Integer index = i; exectuor.submit(new Runnable() { public void run() { conf.addProperty("key", index); conf.addProperty("key", "stringValue"); doneSignal.countDown();//from w w w . j ava2s .com try { Thread.sleep(50); } catch (InterruptedException e) { } } }); } try { doneSignal.await(); } catch (InterruptedException e) { } List prop = (List) conf.getProperty("key"); assertEquals(2000, prop.size()); }
From source file:gobblin.tunnel.TunnelTest.java
@Test public void mustHandleMultipleConnections() throws Exception { mockExample();//from w ww . ja v a 2s.com Tunnel tunnel = Tunnel.build("example.org", 80, "localhost", PORT); int clients = 5; final CountDownLatch startSignal = new CountDownLatch(1); final CountDownLatch doneSignal = new CountDownLatch(clients); ExecutorService executor = Executors.newFixedThreadPool(clients); try { final int tunnelPort = tunnel.getPort(); List<Future<String>> results = new ArrayList<Future<String>>(); for (int i = 0; i < clients; i++) { Future<String> result = executor.submit(new Callable<String>() { @Override public String call() throws Exception { startSignal.await(); try { return fetchContent(tunnelPort); } finally { doneSignal.countDown(); } } }); results.add(result); } startSignal.countDown(); doneSignal.await(); for (Future<String> result : results) { assertNotNull(result.get()); } } finally { tunnel.close(); } }
From source file:com.test.sharksharding.util.sequence.GetMysqlSequenceId2Test.java
/** * ???SequenceId/*from w ww . jav a 2 s. c o m*/ * * @author gaoxianglong */ public @Test void getSequenceId3() { final CountDownLatch count = new CountDownLatch(2); final List<Long> id1 = new ArrayList<Long>(); final List<Long> id2 = new ArrayList<Long>(); final int size = 10000; new Thread() { public void run() { for (int i = 0; i < size; i++) { id1.add(SequenceIDManger.getSequenceId(100, 10, 5000)); } count.countDown(); } }.start(); new Thread() { public void run() { for (int i = 0; i < size; i++) { id2.add(SequenceIDManger.getSequenceId(100, 10, 5000)); } count.countDown(); } }.start(); try { count.await(); Assert.assertFalse(id1.containsAll(id2)); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.microsoft.office.core.EventsAsyncTestCase.java
private void createAndCheck() throws Exception { prepareEvent();/*from w w w . ja v a2 s . co m*/ final CountDownLatch cdl = new CountDownLatch(1); Futures.addCallback(Me.flushAsync(), new FutureCallback<Void>() { @Override public void onFailure(Throwable t) { reportError(t); cdl.countDown(); } @Override public void onSuccess(Void result) { try { assertTrue(StringUtils.isNotEmpty(EventsAsyncTestCase.this.event.getId())); } catch (Throwable t) { reportError(t); } cdl.countDown(); } }); cdl.await(); }
From source file:info.archinnov.achilles.it.TestAsyncDSLSimpleEntity.java
@Test public void should_dsl_select_slice_async() throws Exception { //Given/*from ww w .jav a2 s . c o m*/ final Map<String, Object> values = new HashMap<>(); final long id = RandomUtils.nextLong(0L, Long.MAX_VALUE); values.put("id", id); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); final Date date1 = dateFormat.parse("2015-10-01 00:00:00 GMT"); final Date date9 = dateFormat.parse("2015-10-09 00:00:00 GMT"); values.put("date1", "'2015-10-01 00:00:00+0000'"); values.put("date2", "'2015-10-02 00:00:00+0000'"); values.put("date3", "'2015-10-03 00:00:00+0000'"); values.put("date4", "'2015-10-04 00:00:00+0000'"); values.put("date5", "'2015-10-05 00:00:00+0000'"); values.put("date6", "'2015-10-06 00:00:00+0000'"); values.put("date7", "'2015-10-07 00:00:00+0000'"); values.put("date8", "'2015-10-08 00:00:00+0000'"); values.put("date9", "'2015-10-09 00:00:00+0000'"); scriptExecutor.executeScriptTemplate("SimpleEntity/insert_many_rows.cql", values); final CountDownLatch latch = new CountDownLatch(1); final CassandraLogAsserter logAsserter = new CassandraLogAsserter(); logAsserter.prepareLogLevel(ASYNC_LOGGER_STRING, "%msg - [%thread]%n"); //When final CompletableFuture<List<SimpleEntity>> future = manager.dsl().select().consistencyList().simpleSet() .simpleMap().value().simpleMap().fromBaseTable().where().id_Eq(id).date_Gte_And_Lt(date1, date9) .withResultSetAsyncListener(rs -> { LOGGER.info(CALLED); latch.countDown(); return rs; }).withTracing().getListAsync(); //Then latch.await(); assertThat(future.get()).hasSize(8); logAsserter.assertContains("Called - [achilles-default-executor"); }
From source file:com.microsoft.office.core.FolderAsyncTestCase.java
private void deleteAndCheck() throws Exception { removeFolder();/*from w ww . j av a2 s . com*/ final CountDownLatch cdl = new CountDownLatch(1); Futures.addCallback(Me.getFolders().getAsync(folder.getId()), new FutureCallback<IFolder>() { @Override public void onFailure(Throwable t) { reportError(t); cdl.countDown(); } @Override public void onSuccess(IFolder result) { try { assertNull(result); } catch (Throwable t) { reportError(t); } cdl.countDown(); } }); cdl.await(); }