简体   繁体   English

如何通过信号的最大计数在一段时间内收集值?

[英]How to collect values over a period of time with max count from a signal?

Is there a way to collect values over a period of time with max count from a signal?有没有办法在一段时间内收集信号中的最大计数值? It seems like a combination of collect(every:on:skipEmpty:discardWhenCompleted:) and collect(count:) .它似乎是collect(every:on:skipEmpty:discardWhenCompleted:)collect(count:)的组合。 The resulting signal would send an event at interval seconds apart if the count of accumulated values doesn't reach max count during time interval.如果累积值的计数在时间间隔内未达到最大计数,则生成的信号将每隔几秒发送一个事件。 Otherwise, it will send immediately.否则,它将立即发送。

Question is hard to grasp, but I will try.问题很难掌握,但我会尝试。

If you want to batch emitted values by count and time, you can use bufferTimeout method.如果要按计数和时间批量发送值,可以使用 bufferTimeout 方法。 See documentation here https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#bufferTimeout-int-java.time.Duration-请参阅此处的文档https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#bufferTimeout-int-java.time.Duration-

Some example:一些例子:

void bufferTimeoutTry() throws InterruptedException {
    Flux.interval(Duration.ofMillis(157))
            .filter(time -> time > 20 && time < 38 || time % 5 == 0 || time % 17 == 0)
            .bufferTimeout(5, Duration.ofSeconds(1))
            .doOnNext(list -> {
                // we will get list of items buffered in 1-second period of time, or at most 5 items.
            })
            .subscribe(System.out::println);
    Thread.sleep(30000);
}

Output will be list of items.输出将是项目列表。 Flux.interval is generating sequential long number (ie 1, 2, 3, 4, 5, ...), it is filtered on second line of method (to get some non interval behavior) and than buffer-ed. Flux.interval正在生成连续的长数(即 1、2、3、4、5,...),它在方法的第二行进行过滤(以获得一些非间隔行为),然后进行缓冲。 After buffer, there is no long on stream, but it has changed to list of longs.缓冲区之后,没有 long 在流中,但它已更改为 long 列表。

[0, 5]
[10, 15]
[17, 20, 21, 22, 23]
[24, 25, 26, 27, 28]
[29, 30, 31, 32, 33]
[34, 35, 36, 37, 40]
[45, 50, 51]
[55, 60]
[65, 68, 70]
[75, 80]
[85, 90]
[95, 100]
[102, 105]
[110, 115]
[119, 120, 125]
[130, 135, 136]
[140, 145]
[150, 153, 155]
[160, 165]
[170, 175]
[180, 185]

Is this what you want?这是你想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 信号:收集时间间隔内的值 - Signal: Collect values over time interval 如何设置通知的期限(时间段)? - How to setup period (time period) for Notifications? 进度条显示持续时间占一段时间的百分比 - Progress Bar showing the duration as a percentage of time passed over a period of time 如何在短时间内存储数据? (迅速) - How to store data for a short period of time? (Swift) 如何在计时器中设置到期时间(_:period:scheduler :) - How to set due time in timer(_:period:scheduler:) 如何将数组切片为自定义时间段? - How to slice an array into a custom time period? 如何在一段时间内运行一个动作? - How to run an action for a set period time? Swift iOS-如何在Firebase TransactionBlock上放置计时器以防止其在特定时间段内增加 - Swift iOS - How to put timer on Firebase TransactionBlock to prevent it from increasing within a certain time period Swift 4中带有Date()的递增和递减计时器应在特定时间段后停止 - Count-Up and Count-Down Timer with Date() in Swift 4 should stop after a certain time period 计算从当前时间到特定时间的时间 - count time from current time to specific time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM