news 2026/5/1 5:07:47

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/5/1 4:47:04

未来电网的“大脑”:基于分布式Agent的负荷预测架构全解析

第一章&#xff1a;未来电网智能演进的背景与挑战随着能源结构转型与可再生能源的大规模接入&#xff0c;传统电网正面临前所未有的运行压力。电力系统需要在高比例风电、光伏并网的背景下维持供需平衡&#xff0c;这对电网的灵活性、可靠性和响应速度提出了更高要求。同时&…

作者头像 李华
网站建设 2026/5/1 4:46:46

【设计模式|第九篇】策略模式实战:优雅解耦业务逻辑

策略模式详解什么是策略模式&#xff1f;核心机制现实类比策略模式的优势典型应用场景1. 电商促销系统2. 支付系统实际应用示例电商订单处理图像处理应用策略模式详解 什么是策略模式&#xff1f; 策略模式是一种行为设计模式&#xff0c;其核心思想是&#xff1a;定义一系列…

作者头像 李华
网站建设 2026/5/1 4:43:53

Julia安装使用记录

Julia是科学计算的利器&#xff0c;很多科学计算相关的代码项目是基于Julia开发的。近期要复现Julia相关的项目&#xff0c;速成学了一下Julia&#xff0c;这里简单记录一下。 Julia安装 Julia安装包下载 Julia的入门资料现在挺多的&#xff0c;我是参考这个文档&#xff1a;J…

作者头像 李华
网站建设 2026/5/1 4:43:52

python 获取自己csdn vip可见文章的articleId curlconverter

Convert curl commands to code 复制为curl import requestscookies {uuid_tt_dd: 10_30868056540-1752190682383-488052,fid: 20_16499080400-1752190683451-844434,UserName: njsgcs,UserInfo: 35bfae3693ed441fa1d055996074b62e,UserToken: 35bfae3693ed441fa1d055996074b…

作者头像 李华
网站建设 2026/5/1 4:44:50

vue和springboot框架开发的申家沟村务管理系统_村委会管理系统3bm52uvo

文章目录具体实现截图主要技术与实现手段关于我本系统开发思路java类核心代码部分展示结论源码lw获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;具体实现截图 同行可拿货,招校园代理 vuespringboot_3bm52uvo 村委会管理系统框架开发的…

作者头像 李华