news 2026/5/25 23:20:30

Kotlin协程buffer缓冲池,调度任务执行

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Kotlin协程buffer缓冲池,调度任务执行

Kotlin协程buffer缓冲池,调度任务执行

import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.delay import kotlinx.coroutines.flow.buffer import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.onCompletion import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.runBlocking fun main() { runBlocking { val taskFlow = flow { repeat(10) { it -> val req = Req(it, (Math.random() * 9999).toInt()) emit(req) } } taskFlow.onStart { println("onStart") } .onCompletion { println("onCompletion") } .onEach { it -> println("onEach $it ${System.currentTimeMillis()}") }.buffer(capacity = 3, onBufferOverflow = BufferOverflow.SUSPEND) .collect { it -> it.load() } } } class Req { var id = 0 var priority = 0 var timestamp = 0L constructor(id: Int, priority: Int) { this.id = id this.priority = priority this.timestamp = System.currentTimeMillis() } suspend fun load() { println("$this loading ...") val time = (Math.random() * 100).toLong() delay(time) println("$this loading time cost=$time") } override fun toString(): String { return "Req(id=$id, priority=$priority, timestamp=$timestamp)" } }

输出:

onStart
onEach Req(id=0, priority=9545, timestamp=1765433999255) 1765433999263
onEach Req(id=1, priority=6391, timestamp=1765433999265) 1765433999265
onEach Req(id=2, priority=3125, timestamp=1765433999265) 1765433999265
onEach Req(id=3, priority=5451, timestamp=1765433999265) 1765433999265
onEach Req(id=4, priority=9302, timestamp=1765433999265) 1765433999265
Req(id=0, priority=9545, timestamp=1765433999255) loading ...
Req(id=0, priority=9545, timestamp=1765433999255) loading time cost=41
Req(id=1, priority=6391, timestamp=1765433999265) loading ...
onEach Req(id=5, priority=3988, timestamp=1765433999316) 1765433999316
Req(id=1, priority=6391, timestamp=1765433999265) loading time cost=92
Req(id=2, priority=3125, timestamp=1765433999265) loading ...
onEach Req(id=6, priority=7803, timestamp=1765433999424) 1765433999424
Req(id=2, priority=3125, timestamp=1765433999265) loading time cost=82
Req(id=3, priority=5451, timestamp=1765433999265) loading ...
onEach Req(id=7, priority=1557, timestamp=1765433999516) 1765433999516
Req(id=3, priority=5451, timestamp=1765433999265) loading time cost=57
Req(id=4, priority=9302, timestamp=1765433999265) loading ...
onEach Req(id=8, priority=7678, timestamp=1765433999579) 1765433999579
Req(id=4, priority=9302, timestamp=1765433999265) loading time cost=68
Req(id=5, priority=3988, timestamp=1765433999316) loading ...
onEach Req(id=9, priority=3493, timestamp=1765433999655) 1765433999655
Req(id=5, priority=3988, timestamp=1765433999316) loading time cost=97
Req(id=6, priority=7803, timestamp=1765433999424) loading ...
onCompletion
Req(id=6, priority=7803, timestamp=1765433999424) loading time cost=79
Req(id=7, priority=1557, timestamp=1765433999516) loading ...
Req(id=7, priority=1557, timestamp=1765433999516) loading time cost=19
Req(id=8, priority=7678, timestamp=1765433999579) loading ...
Req(id=8, priority=7678, timestamp=1765433999579) loading time cost=57
Req(id=9, priority=3493, timestamp=1765433999655) loading ...
Req(id=9, priority=3493, timestamp=1765433999655) loading time cost=41

Process finished with exit code 0

相关:

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

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

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

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

C++设计模式大乱斗:让代码不再“乱炖”(一)

文章目录1. 造人派(创建型)1.1 单例模式(Singleton):朕的江山只有一位!干啥用?核心奥义猫哥上代码猫哥点评1.2 工厂模式(Factory):对象量产流水线&#xff01…

作者头像 李华
网站建设 2026/5/26 5:35:24

【time-rs】time-core 中的 convert.rs 文件详解

概述 这个文件是 time-core crate 中的时间单位转换模块,采用编译时计算的零成本抽象设计。它定义了一系列时间单位类型(如纳秒、微秒等)和它们之间的转换关系。 1. 设计哲学 零成本抽象 编译时计算:所有转换系数在编译时确定无运…

作者头像 李华
网站建设 2026/5/25 10:58:15

Wan2.2-T2V-A14B为气象预报节目提供动态可视化素材

Wan2.2-T2V-A14B为气象预报节目提供动态可视化素材你有没有想过,明天的天气预报,可能不是由摄像师、动画师和剪辑师熬夜赶出来的——而是AI在几分钟内“画”出来的?🌧️🎨 就在我们还在讨论“今天出门要不要带伞”的时…

作者头像 李华