本文整理汇总了Java中java.util.concurrent.ForkJoinPool.invokeAll方法的典型用法代码示例。如果您正苦于以下问题:Java ForkJoinPool.invokeAll方法的具体用法?Java ForkJoinPool.invokeAll怎么用?Java ForkJoinPool.invokeAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ForkJoinPool
的用法示例。
在下文中一共展示了ForkJoinPool.invokeAll方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreateKeyMultithreaded
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
@Test
public void testCreateKeyMultithreaded() {
final int count = 100000;
final Collection<Callable<String>> tasks = IntStream.range(0, count).boxed()
.map(i -> (Callable<String>) () -> KeyGenerator.createKey()).collect(Collectors.toList());
final ForkJoinPool pool = ForkJoinPool.commonPool();
final List<Future<String>> results = pool.invokeAll(tasks);
final Set<String> keys = results.stream().map(t -> {
try {
return t.get();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException(e);
}
}).collect(Collectors.toSet());
Assert.assertEquals("If " + count + " key generations are performed in parallel, it should yield " + count
+ " of distinct keys", count, keys.size());
}
示例2: map
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
* Applies the given function to every element of the given collection and returns a Collection of the results.
*
* @param function the function which is applied to every element of collection
* @param collection the elements
* @param <E> Input (argument) type
* @param <R> Result type
* @return a collection of results of the function
* @throws PSRSSException if the parallel execution failed
*/
private <E, R> Collection<R> map(final Function<E, R> function, Iterable<E> collection) throws PSRSSException {
ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
Collection<Callable<R>> tasks = new LinkedList<>();
for (final E item : collection) {
tasks.add(new Callable<R>() {
@Override
public R call() throws Exception {
return function.execute(item);
}
});
}
List<Future<R>> futures = pool.invokeAll(tasks);
Collection<R> results = new ArrayList<>(futures.size());
for (Future<R> future : futures) {
if (!future.isCancelled()) {
try {
results.add(future.get());
} catch (InterruptedException | ExecutionException e) {
throw new PSRSSException(e);
}
} else {
throw new PSRSSException("Parallel execution failed");
}
}
pool.shutdown();
return results;
}
示例3: testTimedInvokeAll5
import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
* timed invokeAll(c) returns results of all completed tasks in c
*/
public void testTimedInvokeAll5() throws Throwable {
ForkJoinPool e = new ForkJoinPool(1);
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(new StringTask());
List<Future<String>> futures
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
}
}