Android面试题

Android面试题之Kotlin多路复用和并发

2024-05-10  本文已影响0人  AntDream

多路复用

await 多路复用

两个API分别从网络和本地缓存取数据,期望哪个先返回就先用哪个

private val filePath = "xxx"
private val gson = Gson()

data class Response<T>(val value:T, val isLocal:Boolean)

fun CoroutineScope.getUserFromLocal(name:String) = async(Dispatchers.IO){
    delay(1000)
    File(filePath).readText().let { gson.fromJson(it, User::class.java) }
}

fun CoroutineScope.getUserFromRemote(name:String) = async(Dispatchers.IO){
    userServiceApi.getUser(name)
}

/**
 * @desc: 多路复用和并发
 */
class CoroutineTest5 {

    @Test
    fun `test select` ()= runBlocking<Unit>{
        GlobalScope.launch {
            val localRequest = getUserFromLocal("xxx")
            val remoteRequest = getUserFromRemote("yyy")
            val userResponse = select<Response<User>> {
                localRequest.onAwait{Response(it, true)}
                remoteRequest.onAwait{Response(it, false)}
            }
            userResponse.value?.let { println(it) }
        }.join()

    }
}

select会返回最先返回的Response

多路复用channel
@Test
fun `test select channel` ()= runBlocking<Unit>{
    val channels = listOf(Channel<Int>(), Channel<Int>())
    GlobalScope.launch {
        delay(200)
        channels[0].send(200)
    }
    GlobalScope.launch {
        delay(100)
        channels[1].send(100)
    }
    val result = select<Int?> {
        channels.forEach{channel ->
            channel.onReceive{it}
        }
    }
    //会输出较快的那个,也就是100
    println(result)

}
哪些事件可以被select
flow实现多路复用
//最终2个结果都会输出
@Test
fun `test select flow` ()= runBlocking<Unit>{
    //模拟实现多路复用
    //函数->协程->flow->flow合并
    val name = "guest"
    coroutineScope {
        listOf(::getUserFromLocal, ::getUserFromRemote)
            .map { function-> function.call(name)}//遍历调用
            .map { deferred-> flow { emit(deferred.await()) } }
            .merge()//多个flow合并成一个flow
            .collect { user->//末端操作符
                println("result:$user")
                println("collect")
            }
    }
}

并发安全

协程的并发工具

```
@Test
fun test safe concurrent tools ()= runBlocking{
    var count = 0
    val mutex = Mutex()
    List(100){
       GlobalScope.launch {
            mutex.withLock {
             count++
           }
       }
    }.joinAll()
    println(count)
}
```
上一篇 下一篇

猜你喜欢

热点阅读