[英]How can I immediately pipe tasks from one ThreadPool to another?
I've got a list of input elements which I want to queue into several ThreadPools. 我有一个输入元素列表,我想排队进入多个ThreadPools。 Let's say this is my input: 假设这是我的输入:
final List<Integer> ints = Stream.iterate(1, i -> i + 1).limit(100).collect(Collectors.toList());
These are the three functions I want the elements to run through each after another: 这些是我希望元素依次运行的三个功能:
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;
};
And these would be the pools for each step: 这些将是每个步骤的池:
final ExecutorService step1Pool = Executors.newFixedThreadPool(4);
final ExecutorService step2Pool = Executors.newFixedThreadPool(3);
final ExecutorService step3Pool = Executors.newFixedThreadPool(1);
I want each element to run through step1Pool
and apply the step1
. 我希望每个元素都可以通过step1Pool
并应用step1
。 As soon as one element is done its result should end up in step2pool
so that step2
can be applied here. 一旦完成一个元素,其结果应在step2pool
结束,以便可以在此处应用step2
。 As soon as something in step2Pool
is done it should be queued in step3Pool
and step3
should be applied. 一旦step2Pool
中的操作完成,就应将其在step3Pool
排队,并应应用step3
。 On my main thread I want to wait until I have all the results from step3
. 在主线程上,我要等到获得step3
所有结果。 The order in which each element is processed doesn't matter. 每个元素的处理顺序无关紧要。 Only that they all run through step1
-> step2
-> step3
on the correct thread pool. 只是他们都通过运行step1
> - step2
- > step3
在正确的线程池。
Basically I want to parallelize the Stream.map
, push each result immediately to the next queue and wait until I've got ints.size()
results from my last thread pool back. 基本上,我想并行化Stream.map
,将每个结果立即推送到下一个队列,然后等待直到从最后一个线程池获得ints.size()
结果为止。
Is there a simple way to do achieve in Java? 有没有简单的方法可以用Java实现?
I believe that CompletableFuture will help you here! 我相信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());
Better use streams for that : 为此,最好使用流:
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.