Flow块的collect会执行吗?

Hel*_*oCW 2 kotlin kotlin-coroutines

我运行代码A并得到结果A。在我看来,它应该是结果B。

看来flow.collect { value -> println(value) }要执行的块。

Flow 块的收集会执行吗?

代码A

fun simple(): Flow<Int> = flow { 
    println("Flow started")
    for (i in 1..3) {
        delay(300)
        emit(i)
    }
}

fun main() = runBlocking<Unit> {
    println("Calling simple function...")
    val flow = simple()
    println("Calling collect...")
    flow.collect { value -> println(value) } //Block?
    println("Calling collect again...")   
}
Run Code Online (Sandbox Code Playgroud)

结果A

Calling simple function...
Calling collect...
Flow started
1
2
3
Calling collect again...
Run Code Online (Sandbox Code Playgroud)

结果B

Calling simple function...
Calling collect...
Flow started
Calling collect again...
1
2
3
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我运行代码 1 并得到了我预期的结果 1。

代码1

fun simple(): Flow<Int> = flow {
    for (i in 1..3) {
        delay(100) 
        emit(i) 
    }
}

fun main() = runBlocking<Unit> {  
    launch {
        for (k in 1..3) {
            println("I'm not blocked $k")
            delay(100)
        }
    }  
    simple().collect { value -> println(value) } 
}
Run Code Online (Sandbox Code Playgroud)

结果1

I'm not blocked 1
1
I'm not blocked 2
2
I'm not blocked 3
3
Run Code Online (Sandbox Code Playgroud)

Ten*_*r04 7

挂起函数不会阻塞,但它们同步的,这意味着协程中的代码执行会等待挂起函数返回,然后再继续。挂起函数调用和阻塞函数调用之间的区别在于,当协程等待挂起函数返回时,线程被释放以用于其他任务。

collect是一个挂起函数,它在内部重复且同步地调用其 lambda(挂起,而不是阻塞),并且在 Flow 完成之前不会返回。

launch是一个启动协程的异步函数。它立即返回,而不等待其协程完成,这就是代码 1 表现如您所料的原因。