简体   繁体   English

如何立即将任务从一个ThreadPool传递到另一个线程池?

[英]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.

相关问题 我可以在ThreadPool中运行后台任务吗? - Can I run background tasks in a ThreadPool? 线程池在android中终止之前,我可以离开线程池多长时间而没有任务? - How long can I leave a threadpool without tasks before it terminates in android? 多个 ScheduledExecutorServices 或一个带有用于固定速率任务的 ThreadPool 的服务 - Multiple ScheduledExecutorServices or one with a ThreadPool for Fixed Rate Tasks 如何确定线程池已完成? - How can I make sure a threadpool is finished? 当一个任务在Threadpool执行程序中返回无效时,如何取消任务中的所有线程 - How to cancel all threads in tasks when one task returns invalid in Threadpool executor 如何知道Java中线程池的任务何时结束 - How to know when the tasks of threadpool is over in java 如何将数据正确地从一项活动传递到另一项活动? - how can I pass data correctly from one activity to another? 如何将 Bitmap 对象从一个活动传递到另一个活动 - How can I pass a Bitmap object from one activity to another 如何将某些值从一个类转移到另一个类? - How can I transfer some values from one class to another? 如何在另一个方法中使用数组中的字符串? - How can I use a string from an array in one method in another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM