Kotlin管道Channel在receiveAsFlow时debounce与flow差异
            
            
              Kotlin
              
              
            
          
          import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
const val timeOut = Long.MAX_VALUE
//val delayTimes = arrayListOf(50L, 100L, 150L, 200L, 250L)
fun main(args: Array<String>) {
    val channel = Channel<String>()
    runBlocking {
        launch(Dispatchers.IO) {
            repeat(5) {
                println("-")
                val t = System.currentTimeMillis()
                channel.send("$it-$t")
                val d = getDelayTime()
                println("$it channel 休眠 $d")
                delay(d)
            }
        }
        launch(Dispatchers.IO) {
            flow {
                repeat(5) {
                    println("--")
                    val t = System.currentTimeMillis()
                    emit("$it-$t")
                    val d = getDelayTime()
                    println("$it flow 休眠 $d")
                    delay(d)
                }
            }.debounce(timeOut) //这里的timeOut值很大,flow的collect收到。
                .collect {
                    println("flow $it")
                }
        }
        channel.receiveAsFlow()
            .debounce(timeOut) //这里的timeOut值很大,collect收不到。
            .collect {
                println("debounce $it")
            } //一直阻塞接收消息
    }
    //阻塞,其实走不到这里。
    channel.close()
}
fun getDelayTime(): Long {
    return 10
}
--
0 channel 休眠 10
0 flow 休眠 10
--
1 flow 休眠 10
1 channel 休眠 10
--
2 channel 休眠 10
2 flow 休眠 10
--
3 channel 休眠 10
3 flow 休眠 10
--
4 channel 休眠 10
4 flow 休眠 10
flow 4-1693561918986
程序运行后,flow很快就收到了最后一条数据4-xxx...,而Channel在receiveAsFlow接收数据debounce时候,将会一直等待下去。这种纯flow和Channel的receiveAsFlow之间的差异要注意。