Kotlin协程Flow流buffer缓冲批量任务或数据,条件筛选任务或数据

Kotlin协程Flow流buffer缓冲批量任务或数据,条件筛选任务或数据

Kotlin 复制代码
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.runBlocking

fun main() {
    runBlocking {
        var eachFlag = true
        var collectFlag = true

        (0..50)
            .chunked(5) //切块,分区
            .asFlow()
            .onStart { println("onStart") }
            .onEach { itList ->
                //假设这里是生产者,密集生产数据或任务

                collectFlag = true

                if (eachFlag) {
                    println("--------------------")
                    eachFlag = false
                }

                print("onEach [ ")
                itList.forEach { itData ->
                    print("$itData ")
                }
                print("]")

                println()
            }
            .buffer(capacity = 3, onBufferOverflow = BufferOverflow.SUSPEND)
            .collect { itList ->
                //假设这里是消费者,这里的消费者以一定的耗时完成任务或消费数据

                eachFlag = true

                if (collectFlag) {
                    println("↓")
                    collectFlag = false
                }

                print("collect [ ")
                itList.forEach { itData ->
                    print("$itData ")
                }
                print("]")

                val sortedList = itList.sortedBy { it }
                print(" 最大值=${sortedList.lastOrNull()} 最小值=${sortedList.firstOrNull()}")

                println()
            }
    }
}

输出:

onStart


onEach [ 0 1 2 3 4 ]

onEach [ 5 6 7 8 9 ]

onEach [ 10 11 12 13 14 ]

onEach [ 15 16 17 18 19 ]

onEach [ 20 21 22 23 24 ]

collect [ 0 1 2 3 4 ] 最大值=4 最小值=0

collect [ 5 6 7 8 9 ] 最大值=9 最小值=5

collect [ 10 11 12 13 14 ] 最大值=14 最小值=10

collect [ 15 16 17 18 19 ] 最大值=19 最小值=15

collect [ 20 21 22 23 24 ] 最大值=24 最小值=20


onEach [ 25 26 27 28 29 ]

onEach [ 30 31 32 33 34 ]

onEach [ 35 36 37 38 39 ]

onEach [ 40 41 42 43 44 ]

onEach [ 45 46 47 48 49 ]

collect [ 25 26 27 28 29 ] 最大值=29 最小值=25

collect [ 30 31 32 33 34 ] 最大值=34 最小值=30

collect [ 35 36 37 38 39 ] 最大值=39 最小值=35

collect [ 40 41 42 43 44 ] 最大值=44 最小值=40

collect [ 45 46 47 48 49 ] 最大值=49 最小值=45


onEach [ 50 ]

collect [ 50 ] 最大值=50 最小值=50

相关:

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

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

相关推荐
Lei活在当下28 分钟前
【日常知识积累】Kotlin let 函数、inline 函数以及 DSL
android·kotlin·编程语言
橙子199110167 小时前
Scaffold
android·kotlin·android jetpack
程序员老刘1 天前
Kotlin vs Dart:当“优雅”变成心智负担,我选择了更简单的 Dart
flutter·kotlin·dart
QING6181 天前
Kotlin协程:Job.cancel() 和 Scope.cancel() 的区别详解!!!
android·kotlin·android jetpack
alexhilton2 天前
Jetpack ViewModel内幕:内部机制与跨平台设计
android·kotlin·android jetpack
QING6182 天前
Kotlin Flow 的 emit 和 tryEmit 有什么区别 ?
android·kotlin·android jetpack
Kapaseker2 天前
面试官最爱问的 Android 数据传递问题
android·kotlin
I'm Jie3 天前
Gradle 多模块依赖集中管理方案,Version Catalogs 详解(Kotlin DSL)
android·java·spring boot·kotlin·gradle·maven
zhangphil3 天前
Kotlin管道Channel融合flow流,协程实现Android废弃的AsyncTaskLoader(A)
kotlin
tangweiguo030519873 天前
Android 插件化开发完全指南(Kotlin DSL/Gradle KTS 配置)
android·kotlin