coroutie_channel6
channel
There are four types of channels
Unlimited channel , Buffered channel , Rendezvous channel , Conflated channel
Rendezvous channel
By default, a “Rendezvous” channel is created.
1 |
|
Watch this video for a better understanding of channels.
https://www.youtube.com/watch?v=HpWQUoVURWQ

channel是并发安全的队列,队列一定存在缓冲区满了,并且没有人调用receive取走函数,send就挂起
produce与actor
复用多个await
多路复用
网络和本地缓存获取数据,哪一个先返回就先用那个做展示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23fun CoroutineScope.getUserFromLocal() = async(Dispatchers.IO) {
// 模拟读取本地数据
delay(1000)
"getUserFromLocal"
}
fun CoroutineScope.getUserFromNetwork() = async(Dispatchers.IO) {
// 模拟读取网络数据
delay(500)
"getUserFromNetwork"
}
fun testSelectAwait() = runBlocking {
GlobalScope.launch {
val userFromLocal = getUserFromLocal()
val userFromNetwork = getUserFromNetwork()
val select = select<String> {
userFromLocal.onAwait { it }
userFromNetwork.onAwait { it }
}
println(select)
}.join()
}
https://kotlinlang.org/docs/coroutines-and-channels.html#channels
coroutie_channel6
https://noteforme.github.io/2022/04/23/coroutie_channel6/