public class ComputeService extends BusinessService {
// 这里演示了一个分布式并行任务,将输入的字符串按空格分解成单词后,提交给集群各节点打印输出
public String displayWords(String text) {
var computer = Cloud.compute();
for (String word : text.split(" ")) {
// 提交计算任务,各节点将分配到的单词打印在控制台
computer.run(() -> System.err.println(word));
}
return "please see the result in the console of your nodes.";
}
// 下面是一个字符统计分布式并行计算实例
public int countWords(String text) {
var computer = Cloud.compute();
// 通过闭包提交计算任务
Collection<Integer> res = computer.apply(String::length, Arrays.asList(text.split(" ")));
// 将各节点返回的计算结果汇总
int total = res.stream().mapToInt(Integer::intValue).sum();
return total;
}
}