news 2026/1/26 12:05:07

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/1/22 16:12:33

【限时揭秘】工业质检Agent精度提升的3个非公开训练技巧

第一章&#xff1a;工业质检Agent精度提升的核心挑战在现代智能制造体系中&#xff0c;工业质检Agent作为自动化质量控制的关键组件&#xff0c;其检测精度直接影响产品良率与生产效率。然而&#xff0c;在实际部署过程中&#xff0c;精度提升面临多重技术与工程挑战。数据质量…

作者头像 李华
网站建设 2026/1/24 20:26:47

基于SpringBoot的墙绘产品展示交易平台的设计与实现(11516)

有需要的同学&#xff0c;源代码和配套文档领取&#xff0c;加文章最下方的名片哦 一、项目演示 项目演示视频 二、资料介绍 完整源代码&#xff08;前后端源代码SQL脚本&#xff09;配套文档&#xff08;LWPPT开题报告&#xff09;远程调试控屏包运行 三、技术介绍 Java…

作者头像 李华
网站建设 2026/1/23 16:50:42

一键追番到 NAS:支持 qBittorrent 与 Aria,番剧更新自动入库

「NAS、键盘、路由器年轻就要多折腾&#xff0c;我是爱折腾的熊猫—多面手博主&#xff01;咱主打的就是一个 “技能不压身&#xff0c;干货不掺水”」引言今天要介绍的依然是新晋项目anime-helper&#xff0c;作为一款追番的工具&#xff0c;anime-helper支持Mikan、Acgrip以及…

作者头像 李华
网站建设 2026/1/23 14:09:45

图像模板匹配技术详解(含 Halcon 实例)

一、基于灰度值的模板匹配1. 基本原理基于灰度值的匹配通过衡量模板图像&#xff08;T&#xff09;与待匹配图像&#xff08;S&#xff09;子区域的灰度相似性实现定位&#xff0c;核心是计算归一化积相关系数&#xff08;NCC&#xff09;&#xff0c;公式如下&#xff1a;(R(i…

作者头像 李华