[英]How to stop execution a Flux after a certain time in Java?
How can the publishing(Streaming) in Flux be stopped After timer e.g. Flux中的发布(流式传输)如何在计时器之后停止,例如1s , if there are still numbers, they will not be published. 1s ,如果还有数字,就不公布了。 eg after 1000 numbers, then they will not be printed.例如,在 1000 个数字之后,它们将不会被打印。
@Test
public void test() {
Flux.range(0, 20000)
// hier
.delayElements(Duration.ofMillis(1))
.parallel(2)
.runOn(Schedulers.parallel())
.doOnNext(i -> {
System.out.println(i);
})
.sequential()
.blockLast();
}
You can use Flux.take(Duration)
to cause the Flux to complete after a certain duration.您可以使用Flux.take(Duration)
使 Flux 在一定时间后完成。
For the code you posted, and a threshold of 1 second, that would look like this:对于您发布的代码和 1 秒的阈值,它看起来像这样:
Flux.range(0, 20000)
// hier
.delayElements(Duration.ofMillis(1))
.parallel(2)
.runOn(Schedulers.parallel())
.doOnNext(i -> {
System.out.println(i);
})
.sequential()
.take(Duration.ofSeconds(1)) // <--
.blockLast();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.