我使用 Spring 反应式作为服务器来进行昂贵的生成并在 Flux 中一一返回结果。如果请求被取消(例如约束和太紧),这具有停止生成的优点。我的代码如下所示:
public Flux<Entity> generate(int nbrOfEntitiesToGenerate, Constaints constraints) {
return Flux.range(0, nbrOfEntitiesToGenerate)
.map(x -> Generator.expensiveGeneration(constraints)
// .subscribeOn(Schedulers.parallel())
;
}
这只完成了我想要的一半,取消时我不会拨打下一个电话expensiveGeneration
,但不会停止当前运行昂贵的生成,如果约束太紧,可能永远不会完成。请问我该怎么做。
额外的问题,如果你知道,我怎样才能在并行中生成 x 个实体以最大限度地利用我的线程(当然不是一次启动所有代)。
提前致谢。