news 2026/2/25 20:19:42

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/2/25 3:26:10

读捍卫隐私03同步

1. 隐私预期1.1. expectation of privacy1.2. 隐私预期应该从传统的纸质信件时代扩展到数字时代1.3. 在美国,执法部门在没有得到收信人许可的情况下不能打开密封的实体信函1.4. 隐私预期是一种司法测试,用于确定美国宪法《第四修正案》声明的隐私保护是否…

作者头像 李华
网站建设 2026/2/24 23:10:41

[Android] B站第三方电视TVapp BV_0.3.10

[Android] 哔哩哔哩第三方电视TVapp BV_0.3.10 链接:https://pan.xunlei.com/s/VOgBfL5mtfupyqvldvMw5dWUA1?pwd45ud# b站官方的TV版使用体验颇受争议,因此许多用户更倾向于使用网上广受好评的第三方开发的TV版本(BBLL)。这款第…

作者头像 李华
网站建设 2026/2/24 7:20:09

【time-rs】 time-core crate 的 Cargo.toml 配置文件详解

概述 time-core 是 time-rs 项目的底层核心库,提供基础的时间算法和数据类型。这个配置文件体现了其作为"内部实现细节"的定位,设计上高度精简且专注于特定用途。 1. 包基本信息分析 包标识与定位 name "time-core" # 明…

作者头像 李华
网站建设 2026/2/25 18:45:31

政府网站与政务新媒体考核指标有什么区别

政府网站与政务新媒体虽然都是数字政府建设的重要组成部分,但由于其载体性质、传播方式和服务定位不同,上级监管部门(如国办、网信办)对二者的考核指标存在显著区别。简单来说,政府网站考核更侧重“功能完备与服务供给…

作者头像 李华
网站建设 2026/2/20 17:53:10

FLUX.1 Kontext终极指南:重新定义AI图像编辑的边界

FLUX.1 Kontext终极指南:重新定义AI图像编辑的边界 【免费下载链接】FLUX.1-Kontext-dev 项目地址: https://ai.gitcode.com/hf_mirrors/black-forest-labs/FLUX.1-Kontext-dev 你是否曾经遇到过这样的困扰:想要精确修改图片中的某个元素&#x…

作者头像 李华
网站建设 2026/2/25 6:08:06

Java新手必看:System类为什么会出现安全警告?

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个交互式Java学习应用,包含:1. System类常见警告的动画演示 2. 可修改的代码沙盒环境 3. 实时错误检测 4. 逐步修复指导 5. 知识测验功能。要求使用Ja…

作者头像 李华