Search in sources :

Example 1 with ListenableFuture

use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.ListenableFuture in project beam by apache.

the class UnboundedReadDeduplicatorTest method cachedIdDeduplicatorMultithreaded.

@Test
public void cachedIdDeduplicatorMultithreaded() throws InterruptedException, ExecutionException {
    byte[] id = new byte[] { -1, 2, 4, 22 };
    UnboundedReadDeduplicator dedupper = CachedIdDeduplicator.create();
    final CountDownLatch startSignal = new CountDownLatch(1);
    int numThreads = 50;
    final CountDownLatch readyLatch = new CountDownLatch(numThreads);
    final CountDownLatch finishLine = new CountDownLatch(numThreads);
    ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    AtomicInteger successCount = new AtomicInteger();
    AtomicInteger noOutputCount = new AtomicInteger();
    List<ListenableFuture<?>> futures = new ArrayList<>();
    for (int i = 0; i < numThreads; i++) {
        futures.add(executor.submit(new TryOutputIdRunnable(dedupper, id, successCount, noOutputCount, readyLatch, startSignal, finishLine)));
    }
    readyLatch.await();
    startSignal.countDown();
    finishLine.await(10L, TimeUnit.SECONDS);
    Futures.allAsList(futures).get();
    executor.shutdownNow();
    // The first thread to run will succeed, and no others will
    assertThat(successCount.get(), equalTo(1));
    // The threads may not all complete; all of the threads that do not succeed must not output
    assertThat(noOutputCount.get(), lessThan(numThreads));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ListenableFuture(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.ListeningExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ListenableFuture (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.ListenableFuture)1 ListeningExecutorService (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.ListeningExecutorService)1 Test (org.junit.Test)1