news 2026/4/23 10:51:40

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/23 11:20:37

金融合规Agent日志深度剖析:如何用日志数据应对SOX、GDPR双重挑战?

第一章&#xff1a;金融合规 Agent 的审计日志在金融行业&#xff0c;系统操作的可追溯性与安全性至关重要。审计日志作为合规性保障的核心组件&#xff0c;能够记录所有关键操作的时间、主体、行为和上下文信息&#xff0c;为监管审查、异常检测和责任追溯提供数据支撑。审计日…

作者头像 李华
网站建设 2026/4/23 11:21:50

LeetCode 451 - 根据字符出现频率排序

文章目录 摘要描述题解答案&#xff08;整体思路&#xff09;第一步&#xff1a;统计字符频率第二步&#xff1a;按频率排序第三步&#xff1a;按排序结果拼接字符串 题解代码&#xff08;Swift 可运行 Demo&#xff09;题解代码分析1. 为什么用 Dictionary 统计&#xff1f;2.…

作者头像 李华
网站建设 2026/4/23 11:21:54

工业机器人精度检测困局突破:基于激光跟踪仪的4维评估体系构建

第一章&#xff1a;工业机器人Agent的精度定义与挑战工业机器人Agent在现代智能制造中承担着装配、焊接、搬运等关键任务&#xff0c;其操作精度直接影响产品质量与生产效率。精度通常分为**绝对精度**和**重复精度**两类&#xff1a;前者指机器人末端执行器到达指定目标点的实…

作者头像 李华
网站建设 2026/4/23 11:22:02

2025年中南大学计算机考研复试机试真题

2025年中南大学计算机考研复试机试真题 2025年中南大学计算机考研复试上机真题 历年中南大学计算机考研复试上机真题 历年中南大学计算机考研复试机试真题 更多学校题目开源地址&#xff1a;https://gitcode.com/verticallimit1/noobdream N 诺 DreamJudge 题库&#xff1…

作者头像 李华
网站建设 2026/4/23 11:21:59

如何用AI新技术绕过老牌业务,比如微信或搜索等?靠范式转移(Paradigm Shift) 去App化,接管入口 合成社交,提供超级情绪价值 生成式媒体,无限个性化 第二大脑,极致隐私与记忆

绕过微信&#xff08;或类似的垄断级Super App&#xff09;的核心逻辑&#xff0c;绝对不是“做一个更好的微信”&#xff0c;而是让“发消息”这个动作本身变得过时或次要。 老牌业务的护城河在于网络效应&#xff08;所有人都在这里&#xff09;和路径依赖&#xff08;习惯&a…

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

Kdenlive v25.08.3:开源多功能视频剪辑软件

Kdenlive v25.08.3 是基于 KDE 与 ffmpeg 开发的开源视频剪辑软件&#xff0c;以简洁界面和强大功能为核心&#xff0c;整合图像调整、颜色校正、音频处理等丰富特效&#xff0c;适配新手入门与进阶用户的多元创作场景&#xff0c;无需付费即可解锁全部核心功能&#xff0c;助力…

作者头像 李华