news 2026/4/16 6:24:22

Kotlin协程flow瞬时密集数据流去重debounce(1)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Kotlin协程flow瞬时密集数据流去重debounce(1)

Kotlin协程flow瞬时密集数据流去重debounce(1)

这个功能很像Android里面利用Handler发送一些列delay的message,然后再handleMessage里面,根据收到的前后时延是否大于某个值,如果大于等于,则处理,否则丢弃。

import kotlinx.coroutines.async import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.newFixedThreadPoolContext import kotlinx.coroutines.runBlocking const val mTimeOut = 300L val mChannel = Channel<Int>() val mThreadPool = newFixedThreadPoolContext(nThreads = 4, name = "my-thread") fun main() { val totalTaskSize = 20 runBlocking { //接收任务 async { mChannel.receiveAsFlow() .onEach { it -> //生产者 println("onEach $it ${Thread.currentThread().name}") }.flowOn(mThreadPool) .debounce(mTimeOut) .collect { it -> //消费者 recv(it) } } //源源不断的密集发送加载任务。 async(mThreadPool) { repeat(totalTaskSize) { it -> println("send $it ${Thread.currentThread().name}") mChannel.send(it) val t = (mTimeOut * Math.random()).toLong() + 100 println("send $it over, delay=$t ${Thread.currentThread().name}") delay(t) } } } } private fun recv(n: Int) { runBlocking { async(mThreadPool) { println("collect $n ${Thread.currentThread().name}") } } }

输出:

send 0 my-thread-1
onEach 0 my-thread-1
send 0 over, delay=276 my-thread-2
send 1 my-thread-3
send 1 over, delay=184 my-thread-3
onEach 1 my-thread-1
send 2 my-thread-4
send 2 over, delay=169 my-thread-4
onEach 2 my-thread-2
send 3 my-thread-3
send 3 over, delay=175 my-thread-3
onEach 3 my-thread-1
send 4 my-thread-4
send 4 over, delay=129 my-thread-4
onEach 4 my-thread-2
send 5 my-thread-2
send 5 over, delay=367 my-thread-2
onEach 5 my-thread-1
collect 5 my-thread-3
send 6 my-thread-2
send 6 over, delay=271 my-thread-2
onEach 6 my-thread-4
send 7 my-thread-1
send 7 over, delay=200 my-thread-1
onEach 7 my-thread-3
send 8 my-thread-2
send 8 over, delay=356 my-thread-2
onEach 8 my-thread-4
collect 8 my-thread-3
send 9 my-thread-2
send 9 over, delay=222 my-thread-2
onEach 9 my-thread-1
send 10 my-thread-4
send 10 over, delay=146 my-thread-4
onEach 10 my-thread-3
send 11 my-thread-2
send 11 over, delay=215 my-thread-2
onEach 11 my-thread-1
send 12 my-thread-2
send 12 over, delay=200 my-thread-2
onEach 12 my-thread-3
send 13 my-thread-1
send 13 over, delay=298 my-thread-1
onEach 13 my-thread-4
send 14 my-thread-2
send 14 over, delay=226 my-thread-2
onEach 14 my-thread-3
collect 13 my-thread-1
send 15 my-thread-2
send 15 over, delay=337 my-thread-2
onEach 15 my-thread-3
collect 15 my-thread-1
send 16 my-thread-2
send 16 over, delay=160 my-thread-2
onEach 16 my-thread-4
send 17 my-thread-2
send 17 over, delay=147 my-thread-2
onEach 17 my-thread-1
send 18 my-thread-3
send 18 over, delay=262 my-thread-3
onEach 18 my-thread-4
send 19 my-thread-2
send 19 over, delay=163 my-thread-2
onEach 19 my-thread-1
collect 19 my-thread-4

相关:

https://blog.csdn.net/zhangphil/article/details/132515686

https://blog.csdn.net/zhangphil/article/details/132525124

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 13:41:39

RN 项目中“页面存在 ≠ 页面可见”会导致哪些隐藏 Bug?

网罗开发 &#xff08;小红书、快手、视频号同名&#xff09; 大家好&#xff0c;我是 展菲&#xff0c;目前在上市企业从事人工智能项目研发管理工作&#xff0c;平时热衷于分享各种编程领域的软硬技能知识以及前沿技术&#xff0c;包括iOS、前端、Harmony OS、Java、Python等…

作者头像 李华
网站建设 2026/4/16 13:34:37

降低 80% 运维成本!有人工业交换机自带远程管理功能

“车间A区交换机断连&#xff0c;赶紧带工具去排查”“凌晨三点远程机房告警&#xff0c;只能驱车两小时赶过去”——这是工业运维工程师的日常。但在某汽车零部件工厂&#xff0c;自从换上有人工业交换机&#xff0c;运维团队的值班电话再也没在深夜响起过。核心原因很简单&am…

作者头像 李华
网站建设 2026/4/16 14:04:55

测试数据管理的AI解决方案

测试数据管理的重要性与挑战 在软件测试领域&#xff0c;测试数据是确保产品质量和稳定性的核心要素。它直接影响测试用例的有效性、缺陷检测率以及整体开发效率。然而&#xff0c;传统测试数据管理&#xff08;TDM&#xff09;方法面临诸多挑战&#xff0c;包括数据稀缺、隐私…

作者头像 李华
网站建设 2026/4/14 4:23:38

Linux CFS(完全公平调度器)原理与实现细节全解析(3)

接前一篇文章&#xff1a;Linux CFS&#xff08;完全公平调度器&#xff09;原理与实现细节全解析&#xff08;2&#xff09; 二、核心概念与关键抽象 2.3 CFS运行队列&#xff08;cfs_rq&#xff09;与红黑树 每个CPU都维护一个就绪队列struct rq&#xff0c;其中CFS使用自己…

作者头像 李华
网站建设 2026/4/16 14:42:48

紧急吃瓜!英伟达GPU供应要缩水了,第一刀砍向RTX 50系列

有消息称英伟达2026上半年要对GeForce RTX 50系列下狠手&#xff0c;产量将一刀砍掉30%-40%。游戏&装机党注意了&#xff01;有消息称英伟达2026上半年要对GeForce RTX 50系列下狠手&#xff0c;产量将一刀砍掉30%-40%。16GB显存的5060 Ti、5070Ti可能最先没……这意味着消…

作者头像 李华