做一电影网站的apk,wordpress更改端口,网站公司备案有用,注册网易免费邮箱Kotlin协程flow瞬时密集数据流去重debounce#xff08;1#xff09; 这个功能很像Android里面利用Handler发送一些列delay的message#xff0c;然后再handleMessage里面#xff0c;根据收到的前后时延是否大于某个值#xff0c;如果大于等于#xff0c;则处理#xff0c…Kotlin协程flow瞬时密集数据流去重debounce1这个功能很像Android里面利用Handler发送一些列delay的message然后再handleMessage里面根据收到的前后时延是否大于某个值如果大于等于则处理否则丢弃。import kotlinx.coroutines.async import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.newFixedThreadPoolContext import kotlinx.coroutines.runBlocking const val mTimeOut 300L val mChannel ChannelInt() val mThreadPool newFixedThreadPoolContext(nThreads 4, name my-thread) fun main() { val totalTaskSize 20 runBlocking { //接收任务 async { mChannel.receiveAsFlow() .onEach { it - //生产者 println(onEach $it ${Thread.currentThread().name}) }.flowOn(mThreadPool) .debounce(mTimeOut) .collect { it - //消费者 recv(it) } } //源源不断的密集发送加载任务。 async(mThreadPool) { repeat(totalTaskSize) { it - println(send $it ${Thread.currentThread().name}) mChannel.send(it) val t (mTimeOut * Math.random()).toLong() 100 println(send $it over, delay$t ${Thread.currentThread().name}) delay(t) } } } } private fun recv(n: Int) { runBlocking { async(mThreadPool) { println(collect $n ${Thread.currentThread().name}) } } }输出send 0 my-thread-1onEach 0 my-thread-1send 0 over, delay276 my-thread-2send 1 my-thread-3send 1 over, delay184 my-thread-3onEach 1 my-thread-1send 2 my-thread-4send 2 over, delay169 my-thread-4onEach 2 my-thread-2send 3 my-thread-3send 3 over, delay175 my-thread-3onEach 3 my-thread-1send 4 my-thread-4send 4 over, delay129 my-thread-4onEach 4 my-thread-2send 5 my-thread-2send 5 over, delay367 my-thread-2onEach 5 my-thread-1collect 5 my-thread-3send 6 my-thread-2send 6 over, delay271 my-thread-2onEach 6 my-thread-4send 7 my-thread-1send 7 over, delay200 my-thread-1onEach 7 my-thread-3send 8 my-thread-2send 8 over, delay356 my-thread-2onEach 8 my-thread-4collect 8 my-thread-3send 9 my-thread-2send 9 over, delay222 my-thread-2onEach 9 my-thread-1send 10 my-thread-4send 10 over, delay146 my-thread-4onEach 10 my-thread-3send 11 my-thread-2send 11 over, delay215 my-thread-2onEach 11 my-thread-1send 12 my-thread-2send 12 over, delay200 my-thread-2onEach 12 my-thread-3send 13 my-thread-1send 13 over, delay298 my-thread-1onEach 13 my-thread-4send 14 my-thread-2send 14 over, delay226 my-thread-2onEach 14 my-thread-3collect 13 my-thread-1send 15 my-thread-2send 15 over, delay337 my-thread-2onEach 15 my-thread-3collect 15 my-thread-1send 16 my-thread-2send 16 over, delay160 my-thread-2onEach 16 my-thread-4send 17 my-thread-2send 17 over, delay147 my-thread-2onEach 17 my-thread-1send 18 my-thread-3send 18 over, delay262 my-thread-3onEach 18 my-thread-4send 19 my-thread-2send 19 over, delay163 my-thread-2onEach 19 my-thread-1collect 19 my-thread-4相关https://blog.csdn.net/zhangphil/article/details/132515686https://blog.csdn.net/zhangphil/article/details/132525124