news 2026/3/21 15:46:51

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/3/20 2:32:14

设备入侵检测规则过时致漏报,后来才知道启用自动规则同步

💓 博客主页:塔能物联运维的CSDN主页目录物联网运维:当代社畜的修罗场 一、物联网运维的"三重门" 二、当运维遇上物联网 三、那些年我们踩过的坑 四、运维人的自我修养 五、未来展望(虽然我也不确定) 六、写…

作者头像 李华
网站建设 2026/3/16 0:11:25

7、网络安全漏洞渗透技术揭秘

网络安全漏洞渗透技术揭秘 1. 引言 在当今数字化时代,网络安全问题日益严峻。黑客们利用各种漏洞渗透技术,试图突破系统防线,获取敏感信息或控制网络设备。本文将深入探讨多种常见的网络安全漏洞渗透技术,包括端口、套接字和服务漏洞渗透,以及相关的攻击方法和防范措施。…

作者头像 李华
网站建设 2026/3/19 17:53:36

Comby:超越正则表达式的智能代码重构利器

Comby:超越正则表达式的智能代码重构利器 【免费下载链接】comby A code rewrite tool for structural search and replace that supports ~every language. 项目地址: https://gitcode.com/gh_mirrors/co/comby 核心价值:重新定义代码搜索与替换…

作者头像 李华
网站建设 2026/3/14 12:57:17

大模型后训练技术详解:微调、对齐与推理优化全解析!

简介 本文详解大模型后训练技术,包括微调(FFT/LoRA)、对齐(RLHF/DPO)和推理优化(CoT/MCTS)等方法,探讨后训练扩展律和测试时计算优化,对比SFT与RL优缺点,为提升大模型性能提供全面指导。 本文探讨了后训练的重要性、方法以及最新…

作者头像 李华
网站建设 2026/3/16 3:14:31

SQL 入门指南:从零开始学习 SQL

当今时代,数据已经成为了我们生活中不可或缺的一部分。无论是企业的经营决策,还是个人的日常消费习惯,都需要通过对数据的收集、分析和应用来实现更好的结果。 而关系型数据库系统,作为最常见的数据存储和管理方式,SQ…

作者头像 李华
网站建设 2026/3/21 8:04:14

Python与Selenium自动化测试完整指南:零基础到实战精通

Python与Selenium自动化测试完整指南:零基础到实战精通 【免费下载链接】Python自动化测试教程完整版PDF 本仓库提供了一份名为“Python自动化测试教程 完整版PDF”的资源文件。该教程专注于使用Python和Selenium进行自动化测试,适合希望快速上手自动化测…

作者头像 李华