【发布时间】:2016-08-06 17:46:24
【问题描述】:
我正在尝试为products 中的每个product 并行调用getPrice 方法。我有这段代码并验证 getPrice 在单独的线程中运行,但它们是按顺序运行的,而不是并行运行的。谁能指出我在这里缺少什么?
非常感谢您的帮助。
ExecutorService service = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
Set<Product> decoratedProductSet = products.stream()
.map(product -> CompletableFuture
.supplyAsync(() -> getPrice(product.getId(), date, context), service))
.map(t -> t.exceptionally(throwable -> null))
.map(t -> t.join())
.collect(Collectors.<Product>toSet());
【问题讨论】:
标签: java-8 executorservice completable-future