Java Code Examples for com.google.common.util.concurrent.ListeningExecutorService#invokeAll()

The following examples show how to use com.google.common.util.concurrent.ListeningExecutorService#invokeAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private static ListenableFuture<List<Path>> submitRenderingTasks(
    ListeningExecutorService executor, Injector injector, Class<? extends Annotation> qualifier)
    throws InterruptedException {
  List<RenderTask> tasks = injector.getInstance(new Key<List<RenderTask>>(qualifier) {});

  @SuppressWarnings("unchecked") // Safe by the contract of invokeAll().
  List<ListenableFuture<Path>> stage1 = (List) executor.invokeAll(tasks);
  return allAsList(stage1);
}
 
Example 2
Source File: Resolver.java    From buck with Apache License 2.0 5 votes vote down vote up
private ImmutableSetMultimap<Path, Prebuilt> downloadArtifacts(
    MutableDirectedGraph<Artifact> graph, ImmutableMap<String, Dependency> specifiedDependencies)
    throws ExecutionException, InterruptedException {
  ListeningExecutorService exec =
      MoreExecutors.listeningDecorator(
          Executors.newFixedThreadPool(
              Runtime.getRuntime().availableProcessors(),
              new MostExecutors.NamedThreadFactory("artifact download")));

  @SuppressWarnings("unchecked")
  List<ListenableFuture<Map.Entry<Path, Prebuilt>>> results =
      (List<ListenableFuture<Map.Entry<Path, Prebuilt>>>)
          (List<?>)
              exec.invokeAll(
                  graph.getNodes().stream()
                      .map(
                          artifact ->
                              (Callable<Map.Entry<Path, Prebuilt>>)
                                  () -> downloadArtifact(artifact, graph, specifiedDependencies))
                      .collect(ImmutableList.toImmutableList()));

  try {
    return ImmutableSetMultimap.<Path, Prebuilt>builder()
        .orderValuesBy(Ordering.natural())
        .putAll(Futures.allAsList(results).get())
        .build();
  } finally {
    exec.shutdown();
  }
}
 
Example 3
Source File: MoreSuppliersTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Test
public void weakMemoizeShouldRunDelegateOnlyOnceOnConcurrentAccess() throws Exception {
  final int numFetchers = 10;

  Semaphore semaphore = new Semaphore(0);

  class TestDelegate implements Supplier<Object> {
    private int timesCalled = 0;

    public int getTimesCalled() {
      return timesCalled;
    }

    @Override
    public Object get() {
      try {
        // Wait for all the fetch threads to be ready.
        semaphore.acquire(numFetchers);
        Thread.sleep(50); // Give other threads a chance to catch up.
        timesCalled++;
        return new Object();
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      } finally {
        semaphore.release(numFetchers);
      }
    }
  }

  TestDelegate delegate = new TestDelegate();
  Supplier<Object> supplier = MoreSuppliers.weakMemoize(delegate);
  ExecutorService threadPool = Executors.newFixedThreadPool(numFetchers);

  try {
    ListeningExecutorService executor = MoreExecutors.listeningDecorator(threadPool);

    class Fetcher implements Callable<Object> {
      @Override
      public Object call() {
        // Signal that this particular fetcher is ready.
        semaphore.release();
        return supplier.get();
      }
    }

    ImmutableList.Builder<Callable<Object>> fetcherBuilder = ImmutableList.builder();
    for (int i = 0; i < numFetchers; i++) {
      fetcherBuilder.add(new Fetcher());
    }

    @SuppressWarnings("unchecked")
    List<ListenableFuture<Object>> futures =
        (List<ListenableFuture<Object>>) (List<?>) executor.invokeAll(fetcherBuilder.build());

    // Wait for all fetchers to finish.
    List<Object> results = Futures.allAsList(futures).get();

    Assert.assertEquals("should only have been called once", 1, delegate.getTimesCalled());
    Assert.assertThat(
        "all result items are the same", ImmutableSet.copyOf(results), Matchers.hasSize(1));

    Preconditions.checkState(
        threadPool.shutdownNow().isEmpty(), "All jobs should have completed");
    Preconditions.checkState(
        threadPool.awaitTermination(10, TimeUnit.SECONDS),
        "Thread pool should terminate in a reasonable amount of time");
  } finally {
    // In case exceptions were thrown, attempt to shut down the thread pool.
    threadPool.shutdownNow();
    threadPool.awaitTermination(10, TimeUnit.SECONDS);
  }
}