当前位置: 首页>>代码示例>>Java>>正文


Java ForkJoinPool.invokeAll方法代码示例

本文整理汇总了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());
}
 
开发者ID:syndesisio,项目名称:syndesis,代码行数:23,代码来源:KeyGeneratorTest.java

示例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;
}
 
开发者ID:woefe,项目名称:xmlrss,代码行数:43,代码来源:PSRedactableSignature.java

示例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());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:ForkJoinPoolTest.java


注:本文中的java.util.concurrent.ForkJoinPool.invokeAll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。