Debounce and pipeline Channel of Kotlin coroutine flow

Debounce and pipeline Channel of Kotlin coroutine flow

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 = 150L
val delayTimes = arrayListOf(50L, 100L, 150L, 200L, 250L)

fun main(args: Array<String>) {
    val channel = Channel<String>()

    runBlocking {
        launch(Dispatchers.IO) {
            repeat(5) {
                val t = System.currentTimeMillis()
                println("待发射 $it $t ...")
                channel.send("$it-$t")

                val d = delayTimes.random() //从列表中随机选择一个延迟时间。
                println("$it 休眠 $d")
                delay(d)
            }
        }

        launch(Dispatchers.IO) {
            channel.receiveAsFlow().debounce(timeOut).collect {
                println("debounce $it")
            }
        }

        println("runBlocking")
    }

    //channel.receiveAsFlow()将永恒处于接收状态,走不到channel.close()这里
    //channel.close()
}

Debounce 0 1693126278615 ...
runBlocking
0 Dormant 100
Debounce 1 1693126278746 ...
1 Dormant 50
Debounce 2 1693126278810 ...
2 Dormant 200
debounce 2-1693126278810
Debounce 3 1693126279011 ...
3 Dormant 50
to be launched 4 1693126279075 . ..
4 sleep 50
debounce 4-1693126279075

        <!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core -->
        <dependency>
            <groupId>org.jetbrains.kotlinx</groupId>
            <artifactId>kotlinx-coroutines-core</artifactId>
            <version>1.7.3</version>
            <type>pom</type>
        </dependency>

https://zhangphil.blog.csdn.net/article/details/131103698icon-default.png?t=N7T8https://zhangphil.blog.csdn.net/article/details/131103698

kotlin coroutine channel channel_zhangphil's blog-CSDN blog runBlocking internally starts 3 coroutines to do time-consuming operations, from the output you can see that 3 coroutines are executed concurrently, and runBlocking will wait until the execution of the 3 coroutines is completed before exiting, output The results are in a clear order. runBlocking will wait for the coroutine of the same scope to complete before exiting runBlocking itself blocks the thread, but the coroutine running inside is not blocked. kotlin's runBlocking code after runBlocking can only be executed after all coroutines of the same scope inside run, and runBlocking will block the thread where it resides. kotlin coroutine pipeline Channel. https://blog.csdn.net/zhangphil/article/details/131096899 Kotlin coroutine flow sending time interval debounce_zhangphil's blog-CSDN blog debounce contains a certain buffering idea, that is, instead of triggering an event immediately, it will first The transmitted data enters the queue, wait for a certain time (time) to delay the trigger, and the important condition of the trigger depends on the time interval between the previous data and the next data. Note that the previous and the next data have not been transmitted yet, but are just waiting. After launching ABC, Kotlin examines the ABCD to be launched. It stands to reason that the interval between ABCD and ABC is 200, which meets the launch timeOut value, but the interval between ABCD and ABCDE is 100, so ABCD is skipped and ABCDE is directly launched. Fourth, map, reorganize and rewrite data. Both A and AB are to be transmitted, and the interval between A and AB is 100, so skip A and directly transmit AB. https://blog.csdn.net/zhangphil/article/details/132515686 kotlin coroutine receiving pipeline ReceiveChannel producer produce_kotlin produce_zhangphil's blog-CSDN blogThe 3 coroutines started inside runBlocking do time-consuming operations. From the output, you can see that the 3 coroutines are executed concurrently. RunBlocking will wait until the execution of the 3 coroutines is completed before exiting. The output results have a clear sequence. runBlocking will wait for the coroutine of the same scope to complete before exiting runBlocking itself blocks the thread, but the coroutine running inside is not blocked. kotlin's runBlocking code after runBlocking can only be executed after all coroutines of the same scope inside run, and runBlocking will block the thread where it resides. kotlin coroutine pipeline Channel. _kotlin produce https://blog.csdn.net/zhangphil/article/details/131103072

Guess you like

Origin blog.csdn.net/zhangphil/article/details/132525124