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);
}
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);
}
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);
}
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);
}
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();
}
Aggregations