Search in sources :

Example 91 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project futuroid by clemp6r.

the class Async method submit.

/**
 * Submits a value-returning task for execution and returns a Future representing the pending results of the task.
 * The task will be run on the provided executor.
 * @param task the task to submit
 * @param executor an {@link java.util.concurrent.ExecutorService} that will be used to run the task
 * @param <T> the result type
 * @return a {@link Future} representing pending completion of the task
 */
public static <T> Future<T> submit(Callable<T> task, ExecutorService executor) {
    ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executor);
    ListenableFuture<T> submit = listeningExecutorService.submit(task);
    return FutureImpl.from(submit);
}
Also used : ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService)

Example 92 with ListeningExecutorService

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

the class CGraphOpener method showGraphAndPerformCallBack.

public static void showGraphAndPerformCallBack(final IViewContainer container, final INaviView view, final CGraphWindow window, final Window parent, final FutureCallback<Boolean> callBack) {
    CWindowManager.instance().bringViewToFront(view);
    final ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
    final ListenableFuture<Boolean> loader = service.submit(generateViewLoader(view, container, window, parent));
    Futures.addCallback(loader, callBack);
}
Also used : ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService)

Example 93 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project jesos by groupon.

the class AbstractTestState method testTenMonkeysPressTenKeys.

@Test
public void testTenMonkeysPressTenKeys() throws Exception {
    final State state = getState();
    final byte[] value = "The quick brown fox jumps over the lazy dog.".getBytes(StandardCharsets.UTF_8);
    final byte[] newValue = "Ich esse Autos zum Abendbrot und mache Kopfsprung ins Sandbecken. Gruen. Rot. Pferderennen.".getBytes(StandardCharsets.UTF_8);
    final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    final ImmutableList.Builder<ListenableFuture<String>> builder = ImmutableList.builder();
    for (int i = 0; i < 10; i++) {
        builder.add(executor.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                final String key = "key-" + UUID.randomUUID().toString();
                final Variable var = state.fetch(key).get();
                assertTrue(var.value().length == 0);
                JVariable storedVar = (JVariable) state.store(var.mutate(value)).get();
                storedVar = (JVariable) state.store(storedVar.mutate(newValue)).get();
                assertNotNull(storedVar);
                final JVariable retrievedVar = (JVariable) state.fetch(key).get();
                assertNotNull(retrievedVar);
                assertArrayEquals(storedVar.value(), retrievedVar.value());
                assertEquals(storedVar.getName(), retrievedVar.getName());
                assertEquals(storedVar.getUuid(), retrievedVar.getUuid());
                return key;
            }
        }));
    }
    final List<String> keys = Futures.allAsList(builder.build()).get();
    for (final String key : keys) {
        final JVariable retrievedVar = (JVariable) state.fetch(key).get();
        assertNotNull(retrievedVar);
        assertArrayEquals(newValue, retrievedVar.value());
    }
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.DAYS);
}
Also used : Variable(org.apache.mesos.state.Variable) State(org.apache.mesos.state.State) ImmutableList(com.google.common.collect.ImmutableList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 94 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project jesos by groupon.

the class AbstractTestState method testTenMonkeysHammerOnTenKeys.

@Test
public void testTenMonkeysHammerOnTenKeys() throws Exception {
    final State state = getState();
    final byte[] value = "The quick brown fox jumps over the lazy dog.".getBytes(StandardCharsets.UTF_8);
    final byte[] newValue = "Ich esse Autos zum Abendbrot und mache Kopfsprung ins Sandbecken. Gruen. Rot. Pferderennen.".getBytes(StandardCharsets.UTF_8);
    final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
    final ImmutableList.Builder<Variable> varBuilder = ImmutableList.builder();
    for (int i = 0; i < 10; i++) {
        final String key = "key-" + UUID.randomUUID().toString();
        final Variable var = state.fetch(key).get();
        assertTrue(var.value().length == 0);
        final Variable storedVar = state.store(var.mutate(value)).get();
        assertNotNull(storedVar);
        builder.add(key);
        varBuilder.add(storedVar);
    }
    final Set<String> keys = builder.build();
    final List<Variable> variables = varBuilder.build();
    final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    final ImmutableList.Builder<ListenableFuture<Integer>> resultBuilder = ImmutableList.builder();
    for (int i = 0; i < 10; i++) {
        resultBuilder.add(executor.submit(new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {
                final ArrayList<Variable> vars = new ArrayList<>(variables);
                Collections.shuffle(vars);
                int updateCount = 0;
                for (final Variable var : vars) {
                    final Variable storedVar = state.store(var.mutate(newValue)).get();
                    if (storedVar != null) {
                        updateCount++;
                        Thread.sleep(2L);
                    }
                }
                return updateCount;
            }
        }));
    }
    final List<Integer> results = Futures.allAsList(resultBuilder.build()).get();
    int finalTally = 0;
    for (final Integer result : results) {
        finalTally += result;
    }
    assertEquals(10, finalTally);
    for (final String key : keys) {
        final Variable retrievedVar = state.fetch(key).get();
        assertNotNull(retrievedVar);
        assertArrayEquals(newValue, retrievedVar.value());
    }
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.DAYS);
}
Also used : Variable(org.apache.mesos.state.Variable) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ImmutableSet(com.google.common.collect.ImmutableSet) State(org.apache.mesos.state.State) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Test(org.junit.Test)

Example 95 with ListeningExecutorService

use of com.google.common.util.concurrent.ListeningExecutorService in project druid by druid-io.

the class LifecycleTest method testConcurrentStartStopOnce.

@Test
public void testConcurrentStartStopOnce() throws Exception {
    final int numThreads = 10;
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(numThreads));
    final Lifecycle lifecycle = new Lifecycle();
    final AtomicLong handlerFailedCount = new AtomicLong(0L);
    final Lifecycle.Handler exceptionalHandler = new Lifecycle.Handler() {

        final AtomicBoolean started = new AtomicBoolean(false);

        @Override
        public void start() {
            if (!started.compareAndSet(false, true)) {
                handlerFailedCount.incrementAndGet();
                throw new ISE("Already started");
            }
        }

        @Override
        public void stop() {
            if (!started.compareAndSet(true, false)) {
                handlerFailedCount.incrementAndGet();
                throw new ISE("Not yet started started");
            }
        }
    };
    lifecycle.addHandler(exceptionalHandler);
    Collection<ListenableFuture<?>> futures = new ArrayList<>(numThreads);
    final AtomicBoolean threadsStartLatch = new AtomicBoolean(false);
    final AtomicInteger threadFailedCount = new AtomicInteger(0);
    for (int i = 0; i < numThreads; ++i) {
        futures.add(executorService.submit(() -> {
            try {
                while (!threadsStartLatch.get()) {
                // await
                }
                lifecycle.start();
            } catch (Exception e) {
                threadFailedCount.incrementAndGet();
            }
        }));
    }
    try {
        threadsStartLatch.set(true);
        Futures.allAsList(futures).get();
    } finally {
        lifecycle.stop();
    }
    Assert.assertEquals(numThreads - 1, threadFailedCount.get());
    Assert.assertEquals(0, handlerFailedCount.get());
    executorService.shutdownNow();
}
Also used : ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ISE(org.apache.druid.java.util.common.ISE) Test(org.junit.Test)

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