当前位置: 首页 > news >正文

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

http://www.cnnetsun.cn/news/9673.html

相关文章:

  • 读捍卫隐私03同步
  • [Android] B站第三方电视TVapp BV_0.3.10
  • 【time-rs】 time-core crate 的 Cargo.toml 配置文件详解
  • 政府网站与政务新媒体考核指标有什么区别
  • FLUX.1 Kontext终极指南:重新定义AI图像编辑的边界
  • Java新手必看:System类为什么会出现安全警告?
  • 基于springboot的大学生实习就业管理系统
  • AXI-A7.4.1 Overview
  • V型翅片与六边形蜂窝翅片的散热性能差异
  • 以太网温湿度传感器五重告警方式如何协同工作?
  • COMSOL介电金属多层膜结构宽谱吸收器:文献复现与吸收特性研究
  • 【必看收藏】LangChain生态实战:LangGraph+LangSmith构建可追踪AI智能体全流程解析
  • 使用DeepSeek开发第一个RAG
  • Jetson Secure Boot 完整实战指南:从 Fuse Key → Boot Chain → 验签代码路径的源码级解析
  • 【LeetCode30_滑动窗口 + 哈希表】:三招搞定“串联所有单词的子串”
  • 以全栈AI能力重塑智能客服服务效能
  • 如何在PHP项目中嵌入Rust代码?5步实现毫秒级响应的高性能服务集成
  • 英伟达推出云端算力集群监管工具,自证GPU无后门
  • 如何用智能配色工具3步打造品牌视觉一致性
  • 【OD刷题笔记】- 分苹果
  • MCP SC-400从入门到精通,构建抗量子攻击防线的关键路径
  • Bigemap Pro水文分析三大核心功能详解:从DEM到精准河网提取
  • Java学习日志--常见类库(上)
  • 直播带货APP开发的核心流程:推流端、观看端与运营端后台搭建指南
  • Wan2.2-T2V-A14B生成火星殖民基地建设构想视频
  • TSF输入法框架开发全指南:从COM组件到拼音输入法落地
  • 在线考试软件哪个好用?
  • 在AWS Athena中使用json_extract_scalar函数对某个json字段进行过滤和分组统计
  • 力扣 22. 括号生成:C++ 实现回溯 + 动态规划双解法,面试高频题必掌握
  • 【开题答辩全过程】以 基于Django的大学生理财及记账系统设计与实现为例,包含答辩的问题和答案