当前位置:网站首页>深潜Kotlin协程(十七):演员
深潜Kotlin协程(十七):演员
2022-06-30 10:09:00 【RikkaTheWorld】
系列电子书:传送门
在计算机科学中,有一种并发模型,称为演员模式(actor model),其最重要的概念就是演员,它是一个计算机实体,响应接收到的消息,它可以并发下面行为:
- 向其他演员发送有限数量的信息
- 创造有限数量的新演员
- 指定接收到下一个信息时应该发生行为
演员可以修改自己的私有状态,但只能通过消息传递间接的影响其它演员,因此不需要同步它们。每个演员运行在单个线程上,一个接一个地处理消息。
在 Kotlin 协程中,我们可以很容易地实现这个模型。我们使用 channel 将消息队列同步到演员上,然后我们只需要一个接一个处理这些消息的协程。在下面的代码片段中,你可以看到我们的 massiveRun 问题用演员模型解决了:
sealed class CounterMsg
object IncCounter : CounterMsg()
class GetCounter(
val response: CompletableDeferred<Int>
) : CounterMsg()
fun CoroutineScope.counterActor(): Channel<CounterMsg> {
val channel = Channel<CounterMsg>()
launch {
var counter = 0
for (msg in channel) {
when (msg) {
is IncCounter -> {
counter++
}
is GetCounter -> {
msg.response.complete(counter)
}
}
}
}
return channel
}
suspend fun main(): Unit = coroutineScope {
val counter: SendChannel<CounterMsg> = counterActor()
massiveRun {
counter.send(IncCounter) }
val response = CompletableDeferred<Int>()
counter.send(GetCounter(response))
println(response.await()) // 1000000
counter.close()
}
这里不存在同步问题,因为演员在单个线程上工作。
为了简化这个模型,有一个 actor 协程构建器,它做了上述所示的事情(创建一个 channel 并启动一个协程),并为异常处理提供了更好的支持(例如,出现异常时会自行关闭 channel)。
fun CoroutineScope.counterActor() = actor<CounterMsg> {
var counter = 0
for (msg in channel) {
when (msg) {
is IncCounter -> counter++
is GetCounter -> msg.response.complete(counter)
}
}
}
总结
演员模型是并行处理的一个重要模型。它目前在 Kotlin 中并不流行,但值得我们去了解,因为有一些案例完全适合使用它们,在这个模型中,最重要的概念是演员,它是响应消息的计算实体。因为它只操作一个协程,所以访问它的状态并没有冲突。我们可以用可能提供持久性或优先级的标准功能包装演员,从而创造许多有趣的可能。
曾经有一段时间,角色模型作为设计后端应用程序的一种主流方法(就像 Akka 一样),但在我看来,这似乎不是现在的趋势,我的观点是,这是因为大多数开发者更喜欢使用特别队列或流软件,如 Kafka 或 RabbitMQ。
边栏推荐
- Kernel linked list (general linked list) "list.h" simple version and individual comments
- 【Rust日报】2021-01-23 几个新库发布
- Double-DQN笔记
- 文件共享服务器
- ionic4 ion-reorder-group组件拖拽改变item顺序
- Auto Seg-Loss: 自动损失函数设计
- SGD有多种改进的形式,为什么大多数论文中仍然用SGD?
- 超长干货 | Kubernetes命名空间详解
- Pytorch notes: validation, model eval V.S torch. no_ grad
- Smith chart view of semi steel coaxial RF line and RF line matching calibration of network analyzer e5071c
猜你喜欢

ArcGIS Pro脚本工具(5)——排序后删除重复项

Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change

Q-Learning笔记

那个程序员,被打了。
[email protected] control a dog's running on OLED"/>Skill combing [email protected] control a dog's running on OLED

数学知识复习:第二型曲线积分

Mysql database foundation: views and variables
[email protected]+阿里云+nbiot+dht11+bh1750+土壤湿度传感器+oled"/>技能梳理[email protected]+阿里云+nbiot+dht11+bh1750+土壤湿度传感器+oled

Arm新CPU性能提升22%,最高可组合12核,GPU首配硬件光追,网友:跟苹果的差距越来越大了...

Getting started with X86 - take over bare metal control
随机推荐
[deep learning] common methods for deep learning to detect small targets
技能梳理[email protected]+阿里云+nbiot+dht11+bh1750+土壤湿度传感器+oled
机器学习面试准备(一)KNN
Typescript – classes in Es5, inheritance, static methods
那个程序员,被打了。
Using LVM to resize partitions
MATLAB image histogram equalization, namely spatial filtering
Voir le changement technologique à travers la Légion Huawei (5): Smart Park
About Library (function library), dynamic library and static library
内存逃逸分析
Smith chart view of semi steel coaxial RF line and RF line matching calibration of network analyzer e5071c
Remember the experience of an internship. It is necessary to go to the pit (I)
File sharing server
mysql数据库基础:TCL事务控制语言
【Rust日报】2021-01-22 首份Rust月刊杂志邀请大家一起参与
转卡通学习笔记
超长干货 | Kubernetes命名空间详解
Go -- standard library sort package
透过华为军团看科技之变(五):智慧园区
Arm新CPU性能提升22%,最高可组合12核,GPU首配硬件光追,网友:跟苹果的差距越来越大了...