当前位置: 首页 > news >正文

Kotlin 协程 - 多路复用 select()

一、概念

        又叫选择表达式,是一个挂起函数,可以同时等待多个挂起结果,只取用最快恢复的那个值(即多种方式获取数据,哪个更快返回结果就用哪个)。

        同时到达 select() 会优先选择先写子表达式,想随机(公平)的话使用 selectUnbiased() 替换 。

        能被选择的都是 SelectClauseN 函数类型。

public suspend inline fun <R> select(crossinline builder: SelectBuilder<R>.() -> Unit): R 

public sealed interface SelectBuilder<in R> {

        public operator fun SelectClause0.invoke(block: suspend () -> R)
        public operator fun <Q> SelectClause1<Q>.invoke(block: suspend (Q) -> R)
        public operator fun <P, Q> SelectClause2<P, Q>.invoke(param: P, block: suspend (Q) -> R)
        public operator fun <P, Q> SelectClause2<P?, Q>.invoke(block: suspend (Q) -> R): Unit = invoke(null, block)
}

SelectClause0对应事件没有返回值。例如 job.onJoin。
SelectClause1对应事件有返回值。例如 deffered.onAwait 和 channel.onReceive。
SelectClause2对应事件有返回值。此外还需要一个额外的参数,例如 Channel.onSend() 有两个参数,第一个是 Channel 数据类型的值表示即将发送的值,第二个是发送成功时的回调函数。

二、使用

在使用 async() 启动协程的返回类型 Deferred 中,定义了 SelectClause1 函数类型的变量 onAwait,其作用和 await() 一样,只是当其在 select() 中作为子语句时,具有“同时等待看谁最先返回”的效果。同理其它。

2.1 复用多个 job.onJoin

fun main() = runBlocking<Unit> {val job1 = launch {delay(100)println("job 1")}val job2 = launch {delay(10)println("job 2")}select {job1.onJoin { println("job 1 更快") }job2.onJoin { println("job 2 更快") }}delay(1000)
}
//打印:
//job 2
//job 2 更快
//job 1

2.2 复用多个 deffered.onAwait

public interface Deferred<out T> : Job {
        public val onAwait: SelectClause1<T>        //等效await()
fun main() = runBlocking {val defferedCache = async {delay(10)"Cache"}val defferedLocal = async {delay(100)"Local"}val defferedRemote = async {delay(1000)"Remote"}val result = select {defferedCache.onAwait { println("最快的是$it") }defferedLocal.onAwait { println("最快的是$it") }defferedRemote.onAwait { println("最快的是$it") }}delay(2000)println(result) //打印:最快的是Cache
}

2.3 复用多个 channel.onReceive

public interface SendChannel<in E> {

        public val onSend: SelectClause2<E, SendChannel<E>>        //等效send()

}

public interface ReceiveChannel<out E> {

        public val onReceive: SelectClause1<E>        //等效receive()

        public suspend fun receiveCatching(): ChannelResult<E>        //等效receiveCatching()

}

//select() 中的 onReceive() 在已经关闭的通道执行会发生失败,并导致相应的 select() 抛出异常,使用 onReceiveCatching() 在关闭通道时执行特定操作。

suspend fun getDataFromLocal() = withContext(Dispatchers.IO) { "Local" }
suspend fun getDataFromRemote() = withContext(Dispatchers.IO) { "Remote" }@OptIn(ExperimentalCoroutinesApi::class)
fun main() = runBlocking {val produceLocal = produce { send(getDataFromLocal()) }val produceRemote = produce { send(getDataFromRemote()) }val result = select {produceLocal.onReceive { it }produceRemote.onReceive { it }}
//    val result = select {
//        produceLocal.onReceiveCatching { it.getOrNull() ?: "Channel已关闭:produceLocal" }
//        produceRemote.onReceiveCatching { it.getOrNull() ?: "Channel已关闭:produceRemote " }
//    }println("结果更快的是:$result")
}
http://www.lryc.cn/news/148423.html

相关文章:

  • 学习笔记-ThreadLocal
  • python利用pandas统计分析—groupby()函数的使用
  • OPENCV实现ORB特征检测
  • W5100S-EVB-PICO主动PING主机IP检测连通性(十)
  • 使用 Nginx 搭建文件下载服务器
  • 链式栈StackT
  • Fiddler中 AutoResponder 使用
  • 77GHz线性调频连续波雷达
  • YOLOV8改进:更换为MPDIOU,实现有效涨点
  • BookStack开源免费知识库docker-compose部署
  • Linux:编译遇到 Please port gnulib freadahead.c to your platform ,怎么破
  • three.js(三):three.js的渲染结构
  • 客户端读写HBase数据库的运行原理
  • 不使用VH6501设备,通过VN1630等普通设备使用canConfigureBusOff函数进行busoff干扰测试
  • 服务器数据恢复-服务器RAID6硬盘故障离线的数据恢复案例
  • DB2 HADR+TSA运维,TSA添加资源组的命令
  • LeetCode-135-分发糖果
  • Viva Workplace Analytics Employee Feedback SU Viva Glint部署方案
  • ASIC-WORLD Verilog(14)系统任务
  • 两台电脑共享文件设置
  • 《C和指针》笔记17:sizeof
  • 说说大表关联小表
  • Unity 之 方括号[ ] 的用法以及作用
  • 微服务nacos或者yml配置内容部分加密jasypt
  • Vue:插槽,与自定义事件
  • Window11-Ubuntu双系统安装
  • 【React】React学习:从初级到高级(一)
  • Flutter 安装教程 + 运行教程
  • 正中优配:A股早盘三大股指微涨 华为概念表现活跃
  • SAP MM学习笔记26- SAP中 振替转记(转移过账)和 在库转送(库存转储)4- Plant间在库转送 之 在库转送Order(有出荷)