news 2026/5/1 11:50:01

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/1 6:10:37

创客匠人 2025 万人峰会实录:AI 智能体重构创始人 IP 变现逻辑 —— 从 0 到年入千万的实战路径

2025 年 11 月 22 日 - 25 日,由创客匠人主办的 “2025 全球创始人 IPAI 万人高峰论坛” 在厦门盛大召开。作为知识变现领域的年度盛会,本次大会以 “AI、IP、未来、共创、格局” 为核心主题,吸引了全球超万名创始人、行业领袖与 IP 从业者齐…

作者头像 李华
网站建设 2026/4/30 10:01:46

系统编程之进程

一,定义进程是一个程序执行的过程,会去分配内存资源,cpu的资源。1.PCB是一个结构体,process control block。系统用于描述正在运行的进程的相关(所有)信息。2.pcb常见内容PID,进程标识符当前工作路径 chdirumask 0002进程打开的文…

作者头像 李华
网站建设 2026/4/30 21:26:53

鸿蒙 Flutter 支付安全:TEE 可信环境下的支付校验实战

1. 引言:为什么需要 TEE 保障鸿蒙 Flutter 支付安全? 随着鸿蒙(HarmonyOS)生态的快速发展,越来越多跨平台应用基于 Flutter 开发并部署到鸿蒙设备上,其中支付场景的安全性尤为关键。支付过程中&#xff0c…

作者头像 李华
网站建设 2026/4/22 18:31:09

利用 PHPStudy(Mac 版)部署 Nuxt3 node-server 模式项目完整教程

利用 PHPStudy(Mac 版)部署 Nuxt3 node-server 模式项目完整教程 一、教程概述 本文针对 Mac 系统下的 PHPStudy(含小皮面板 XP.CN),详细讲解如何部署 Nuxt3 node-server 模式产物。node-server 模式是 Nuxt3 默认的服…

作者头像 李华
网站建设 2026/4/23 14:07:39

LP3524B高耐压200V隔离型LLC同步整流控制芯片典型电路

MP6924A 在 48 V 适配器、LED TV 电源里已大量采用&#xff0c;但 2024 年 Q3 起交期 20 周&#xff0c;价格翻 1.5 倍。芯茂微推出的 LP3524B 脚位完全兼容&#xff0c;关键参数全面胜出&#xff1a;耐压 200 V&#xff08;vs 200 V 相同&#xff09;、关断速度 < 50 ns&am…

作者头像 李华
网站建设 2026/4/29 8:28:58

PHP 可用的函数

PHP 可用的函数 引言 PHP 是一种广泛使用的开源服务器端脚本语言,它具有丰富的函数库,使得开发者能够轻松地构建各种动态网站和应用程序。本文将详细介绍 PHP 中一些常见的函数,并对其用途和用法进行详细说明。 一、基础函数 1.1 echo 和 print echo 和 print 是 PHP 中…

作者头像 李华