如何编写代码来执行并行分组减少

问题描述

我想编写一个 Reactor 查询来对整数输入流执行并行 groupby 聚合,其中键由 x -> x % 100 选择。

为了说明,查询应如下工作:

(1) 查询应指定将创建多少工作线程。

(2) 相同key的item应该交给同一个worker。

(3) 消费者收集工作人员的输出,并在输入终止时终止程序(并收集工作人员发送的所有项目)。

我应该如何编写这样的查询

我尝试了下面的代码,但与顺序查询相比,并行查询没有提供加速(假设工作线程的数量numOfWorkers):

final ExecutorService svc = Executors.newFixedThreadPool(numOfWorkers);
// consumer prints out the received input items
inputStream.groupBy(x -> x % numOfKeys)
           .map(g -> g.publishOn(Schedulers.fromExecutorService(svc))
           .reduce(0L,(s,x) -> {
               // some complex commutative computation here                
            }).flux())
           .doAfterTerminate(svc::shutdown)
           .subscribe(s -> s.subscribe(consumer));

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)