内容简介:Essentially,This example
Kotlin Actors are part of the Kotlin Coroutines library. I’ll walk you though the reasons why I use Kotlin Actors to achieve concurrency, while leveraging Coroutines to process reactive events in unknown order.
Concurrency?
- Allows events to happen out-of-order or in partial order, without affecting the final outcome.
- This allows for leveraging parallel execution without giving up determinism.
Why Does Android need Reactive Programming?
- Click Events
- Intents
- Networking Requests
- Disk Writes
- etc.
Kotlin Coroutines?
Essentially, Kotlin Coroutines are light-weight threads. They are launched in a context of some CoroutineScope.
Kotlin Actors?
- A Single Kotlin Coroutine
- Processes incoming Messages
- Backed by a Channel
- Concurrent
Actors receive Messages (Intentions) via a Channel
Channels are the only way to safely communicate across Coroutines.
This example implements a Shopping Cart Dao from my GitHub Project ShoppingApp . I’ve created a type called Intention which are sent across the channel as messages. The intentions represent actions I want to perform, but keep my data thread safe.
sealed class Intention {
class FindByLabel(
val label: String,
val deferred: CompletableDeferred<ItemWithQuantity?>
) : Intention()
class Upsert(val itemWithQuantity: ItemWithQuantity) : Intention()
class Remove(val itemWithQuantity: ItemWithQuantity) : Intention()
object Empty : Intention()
}
Actors Process Messages Sequentially in a for() loop
These messages (Intentions) come in across a Channel from other Coroutines, get queued, and then get processed by the Actor sequentially to achieve concurrency.
scope.actor<Intention> {
for (intention in channel) {
// Process Messages/Intentions
when (intention) {
is Intention.FindByLabel -> {
// ...
}
is Intention.Upsert -> {
// ...
}
is Intention.Remove -> {
// ...
}
is Intention.Empty -> {
// ...
}
}
}
}
Sending Messages to the Actor – send() vs offer()
To send messages to the actor, you send a message to it using actor.send(intention) or actor.offer(intention) . Here are the differences between them ( from the Kotlin documentation of SendChannel ).
CompletableDeferred to await() Results
We send in messages to the actor, but sometimes we want to wait for a result once the message has been processed by the actor. We use CompletableDeferred to do this. We await() the result, like in this example where we are querying for a value:
class FindByLabel(
val label: String,
val deferred: CompletableDeferred<ItemWithQuantity?>
) : Intention()
// ---
val deferred = CompletableDeferred<ItemWithQuantity?>()
actor.send(
Intention.FindByLabel(
label = label,
deferred = deferred
)
)
val result : ItemWithQuantity? = deferred.await()
Aren’t Actors Marked with @ObsoleteCoroutineApi?
Yes, but complex actors will also support the same use cases, and there will be a clear path. Also, there is no planned replacement at this point. See the response from the Kotlin Coroutines tech lead from the GitHub issue :
Slides & Video (Coming Soon)
I was able to present this to Boston Android meetup group and 18 other meetup groups on Tuesday, July 14th which was an amazing experience. The video will be available soon and I’ll be sure to put it here. Here are the slides for now.
Links
- Kotlin Coroutines: https://kotlinlang.org/docs/reference/coroutines-overview.html
- Kotlin Actors: https://kotlinlang.org/docs/reference/coroutines/shared-mutable-state-and-concurrency.html#actors
- Presentation Slides: https://speakerdeck.com/handstandsam/kotlin-actors-no-drama-concurrency
- Meetup: https://www.meetup.com/boston-android/events/271483073/
以上所述就是小编给大家介绍的《Kotlin Actors – No Drama Concurrency》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
离散数学及其应用(原书第7版)
Kenneth H. Rosen / 徐六通、杨娟、吴斌 / 机械工业出版社 / 2015-1-1 / 129
《计算机科学丛书:离散数学及其应用(原书第7版)》是介绍离散数学理论和方法的经典教材,已经成为采用率最高的离散数学教材,被美国众多名校用作教材,获得了极大的成功。中文版也已被国内大学广泛采用为教材。作者参考使用教师和学生的反馈,并结合自身对教育的洞察,对第7版做了大量的改进,使其成为更有效的教学工具。《计算机科学丛书:离散数学及其应用(原书第7版)》可作为1至2个学期的离散数学课入门教材,适用于数......一起来看看 《离散数学及其应用(原书第7版)》 这本书的介绍吧!