Search in sources :

Example 31 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project jackrabbit-oak by apache.

the class ConcurrentReadIT method concurrentNodeIteration.

@Test
public void concurrentNodeIteration() throws RepositoryException, InterruptedException, ExecutionException {
    final Session session = createAdminSession();
    try {
        final Node testRoot = session.getRootNode().addNode("test-root");
        for (int k = 0; k < 50; k++) {
            testRoot.addNode("n" + k);
        }
        session.save();
        ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
        List<ListenableFuture<?>> futures = Lists.newArrayList();
        for (int k = 0; k < 20; k++) {
            futures.add(executorService.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    for (int k = 0; k < 10000; k++) {
                        session.refresh(false);
                        NodeIterator children = testRoot.getNodes();
                        children.hasNext();
                    }
                    return null;
                }
            }));
        }
        // Throws ExecutionException if any of the submitted task failed
        Futures.allAsList(futures).get();
        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.DAYS);
    } finally {
        session.logout();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Callable(java.util.concurrent.Callable) Session(javax.jcr.Session) Test(org.junit.Test)

Example 32 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project bazel by bazelbuild.

the class JarFilter method parseJavaFiles.

/** Finds the expected jar archive file name prefixes for the java files. */
static List<String> parseJavaFiles(List<Path> javaFiles) throws IOException {
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
    List<ListenableFuture<String>> futures = Lists.newArrayList();
    for (final Path javaFile : javaFiles) {
        futures.add(executorService.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                String packageString = getDeclaredPackageOfJavaFile(javaFile);
                return packageString != null ? getArchiveFileNamePrefix(javaFile.toString(), packageString) : null;
            }
        }));
    }
    try {
        List<String> archiveFileNamePrefixes = Futures.allAsList(futures).get();
        List<String> result = Lists.newArrayList();
        for (String archiveFileNamePrefix : archiveFileNamePrefixes) {
            if (archiveFileNamePrefix != null) {
                result.add(archiveFileNamePrefix);
            }
        }
        return result;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IOException(e);
    } catch (ExecutionException e) {
        throw new IOException(e);
    }
}
Also used : Path(java.nio.file.Path) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable)

Example 33 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project bazel by bazelbuild.

the class PackageParser method parsePackageStrings.

@Nonnull
@VisibleForTesting
public Map<ArtifactLocation, String> parsePackageStrings(@Nonnull List<ArtifactLocation> sources) throws Exception {
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
    Map<ArtifactLocation, ListenableFuture<String>> futures = Maps.newHashMap();
    for (final ArtifactLocation source : sources) {
        futures.put(source, executorService.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                return getDeclaredPackageOfJavaFile(source);
            }
        }));
    }
    Map<ArtifactLocation, String> map = Maps.newHashMap();
    for (Entry<ArtifactLocation, ListenableFuture<String>> entry : futures.entrySet()) {
        String value = entry.getValue().get();
        if (value != null) {
            map.put(entry.getKey(), value);
        }
    }
    return map;
}
Also used : ArtifactLocation(com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.ArtifactLocation) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Callable(java.util.concurrent.Callable) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nonnull(javax.annotation.Nonnull)

Example 34 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project zipkin by openzipkin.

the class DeduplicatingExecutorTest method exceptionArentCached_deferredFuture.

@Test
public void exceptionArentCached_deferredFuture() throws Exception {
    ListeningExecutorService exec = listeningDecorator(Executors.newSingleThreadExecutor());
    try {
        executor = TestDeduplicatingExecutor.create(s -> {
            if (s == first) {
                return exec.submit(() -> {
                    Thread.sleep(50);
                    throw new IllegalArgumentException();
                });
            }
            return Futures.immediateFuture(s);
        });
        exceptionsArentCached();
    } finally {
        exec.shutdownNow();
    }
}
Also used : MoreExecutors.listeningDecorator(com.google.common.util.concurrent.MoreExecutors.listeningDecorator) ImmutableSet(com.google.common.collect.ImmutableSet) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Reflection(com.google.common.reflect.Reflection) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Futures(com.google.common.util.concurrent.Futures) BoundStatement(com.datastax.driver.core.BoundStatement) Session(com.datastax.driver.core.Session) ExecutorService(java.util.concurrent.ExecutorService) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Assertions.failBecauseExceptionWasNotThrown(org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown) Mockito.mock(org.mockito.Mockito.mock) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Test(org.junit.Test)

Example 35 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project bazel by bazelbuild.

the class AndroidResourceProcessor method deserializeSymbolsToData.

/** Deserializes a list of serialized resource paths to a {@link ParsedAndroidData}. */
public ParsedAndroidData deserializeSymbolsToData(List<Path> symbolPaths) throws IOException, MergingException {
    AndroidDataDeserializer deserializer = AndroidDataDeserializer.create();
    final ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(15));
    final Builder deserializedDataBuilder = ParsedAndroidData.Builder.newBuilder();
    try (Closeable closeable = ExecutorServiceCloser.createWith(executorService)) {
        List<ListenableFuture<Boolean>> deserializing = new ArrayList<>();
        for (final Path symbolPath : symbolPaths) {
            deserializing.add(executorService.submit(new Deserialize(deserializer, symbolPath, deserializedDataBuilder)));
        }
        FailedFutureAggregator<MergingException> aggregator = FailedFutureAggregator.createForMergingExceptionWithMessage("Failure(s) during dependency parsing");
        aggregator.aggregateAndMaybeThrow(deserializing);
    }
    return deserializedDataBuilder.build();
}
Also used : Path(java.nio.file.Path) MergingException(com.android.ide.common.res2.MergingException) Builder(com.google.devtools.build.android.ParsedAndroidData.Builder) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService)

Aggregations

ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)201 Test (org.junit.Test)115 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)75 ArrayList (java.util.ArrayList)43 CountDownLatch (java.util.concurrent.CountDownLatch)29 ExecutorService (java.util.concurrent.ExecutorService)28 IOException (java.io.IOException)25 ExecutionException (java.util.concurrent.ExecutionException)25 Interval (org.joda.time.Interval)25 DateTime (org.joda.time.DateTime)23 List (java.util.List)21 Callable (java.util.concurrent.Callable)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 DruidServer (io.druid.client.DruidServer)18 DataSegment (io.druid.timeline.DataSegment)18 DruidServer (org.apache.druid.client.DruidServer)17 ImmutableMap (com.google.common.collect.ImmutableMap)16 File (java.io.File)16 Map (java.util.Map)16 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)15