[英]How can I immediately pipe tasks from one ThreadPool to another?
我有一个输入元素列表,我想排队进入多个ThreadPools。 假设这是我的输入:
final List<Integer> ints = Stream.iterate(1, i -> i + 1).limit(100).collect(Collectors.toList());
这些是我希望元素依次运行的三个功能:
final Function<Integer, Integer> step1 =
value -> { // input from the ints list
return value * 2;
};
final Function<Integer, Double> step2 =
value -> { // input from the previous step1
return (double) (value * 2); //
};
final Function<Double, String> step3 =
value -> { // input from the previous step2
return "Result: " + value * 2;
};
这些将是每个步骤的池:
final ExecutorService step1Pool = Executors.newFixedThreadPool(4);
final ExecutorService step2Pool = Executors.newFixedThreadPool(3);
final ExecutorService step3Pool = Executors.newFixedThreadPool(1);
我希望每个元素都可以通过step1Pool
并应用step1
。 一旦完成一个元素,其结果应在step2pool
结束,以便可以在此处应用step2
。 一旦step2Pool
中的操作完成,就应将其在step3Pool
排队,并应应用step3
。 在主线程上,我要等到获得step3
所有结果。 每个元素的处理顺序无关紧要。 只是他们都通过运行step1
> - step2
- > step3
在正确的线程池。
基本上,我想并行化Stream.map
,将每个结果立即推送到下一个队列,然后等待直到从最后一个线程池获得ints.size()
结果为止。
有没有简单的方法可以用Java实现?
我相信CompletableFuture将在这里为您提供帮助!
List<CompletableFuture<String>> futures = ints.stream()
.map(i -> CompletableFuture.supplyAsync(() -> step1.apply(i), step1Pool)
.thenApplyAsync(step2, step2Pool)
.thenApplyAsync(step3, step3Pool))
.collect(Collectors.toList());
List<String> result = futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
为此,最好使用流:
List<String> stringList = Stream.iterate(1, i -> i + 1)
.limit(100)
.parallel()
.map(step1)
.map(step2)
.map(step3)
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.