@@ -1068,7 +1068,7 @@ and update data, do animations, etc. All of these coroutines must be cancelled w
10681068to avoid memory leaks.
10691069
10701070We can manage a lifecycle of our coroutines by creating an instance of [ Job] that is tied to
1071- the lifecycle of our activity. A job instance is created using [ Job()] [ Job.invoke ] factory function
1071+ the lifecycle of our activity. A job instance is created using [ ` Job() ` ] [ Job ] factory function
10721072as the following example shows. We need to make sure that all the coroutines are started
10731073with this job in their context and then a single invocation of [ Job.cancel] terminates them all.
10741074
@@ -1458,7 +1458,7 @@ The channels shown so far had no buffer. Unbuffered channels transfer elements w
14581458meet each other (aka rendezvous). If send is invoked first, then it is suspended until receive is invoked,
14591459if receive is invoked first, it is suspended until send is invoked.
14601460
1461- Both [ Channel()] [ Channel.invoke ] factory function and [ produce] builder take an optional ` capacity ` parameter to
1461+ Both [ ` Channel() ` ] [ Channel ] factory function and [ produce] builder take an optional ` capacity ` parameter to
14621462specify _ buffer size_ . Buffer allows senders to send multiple elements before suspending,
14631463similar to the ` BlockingQueue ` with a specified capacity, which blocks when buffer is full.
14641464
@@ -1790,31 +1790,47 @@ There is an [actor] coroutine builder that conveniently combines actor's mailbox
17901790scope to receive messages from and combines the send channel into the resulting job object, so that a
17911791single reference to the actor can be carried around as its handle.
17921792
1793+ The first step of using an actor is to define a class of messages that an actor is going to process.
1794+ Kotlin's [ sealed classes] ( https://kotlinlang.org/docs/reference/sealed-classes.html ) are well suited for that purpose.
1795+ We define ` CounterMsg ` sealed class with ` IncCounter ` message to increment a counter and ` GetCounter ` message
1796+ to get its value. The later needs to send a response. A [ CompletableDeferred] communication
1797+ primitive, that represents a single value that will be known (communicated) in the future,
1798+ is used here for that purpose.
1799+
17931800``` kotlin
17941801// Message types for counterActor
17951802sealed class CounterMsg
17961803object IncCounter : CounterMsg() // one-way message to increment counter
1797- class GetCounter (val response : SendChannel <Int >) : CounterMsg() // a request with reply
1804+ class GetCounter (val response : CompletableDeferred <Int >) : CounterMsg() // a request with reply
1805+ ```
17981806
1807+ Then we define a function that launches an actor using an [ actor] coroutine builder:
1808+
1809+ ``` kotlin
17991810// This function launches a new counter actor
18001811fun counterActor () = actor<CounterMsg >(CommonPool ) {
18011812 var counter = 0 // actor state
18021813 for (msg in channel) { // iterate over incoming messages
18031814 when (msg) {
18041815 is IncCounter -> counter++
1805- is GetCounter -> msg.response.send (counter)
1816+ is GetCounter -> msg.response.complete (counter)
18061817 }
18071818 }
18081819}
1820+ ```
1821+
1822+ The main code is straightforward:
18091823
1824+ ``` kotlin
18101825fun main (args : Array <String >) = runBlocking<Unit > {
18111826 val counter = counterActor() // create the actor
18121827 massiveRun(CommonPool ) {
18131828 counter.send(IncCounter )
18141829 }
1815- val response = Channel <Int >()
1830+ // send a message to get a counter value from an actor
1831+ val response = CompletableDeferred <Int >()
18161832 counter.send(GetCounter (response))
1817- println (" Counter = ${response.receive ()} " )
1833+ println (" Counter = ${response.await ()} " )
18181834 counter.close() // shutdown the actor
18191835}
18201836```
@@ -2190,7 +2206,7 @@ Channel was closed
21902206[ launch ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/launch.html
21912207[ delay ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/delay.html
21922208[ runBlocking ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/run-blocking.html
2193- [ Job ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/index .html
2209+ [ Job ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job.html
21942210[ CancellationException ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-cancellation-exception.html
21952211[ yield ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/yield.html
21962212[ CoroutineScope.isActive ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope/is-active.html
@@ -2209,20 +2225,19 @@ Channel was closed
22092225[ Unconfined ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-unconfined/index.html
22102226[ newCoroutineContext ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-coroutine-context.html
22112227[ CoroutineName ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-name/index.html
2212- [ Job.invoke ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/invoke.html
22132228[ Job.cancel ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/cancel.html
2229+ [ CompletableDeferred ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-completable-deferred.html
22142230<!-- - INDEX kotlinx.coroutines.experimental.sync -->
2215- [ Mutex ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/index .html
2231+ [ Mutex ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex.html
22162232[ Mutex.lock ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/lock.html
22172233[ Mutex.unlock ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/unlock.html
22182234<!-- - INDEX kotlinx.coroutines.experimental.channels -->
2219- [ Channel ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel/index .html
2235+ [ Channel ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel.html
22202236[ SendChannel.send ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/send.html
22212237[ ReceiveChannel.receive ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/receive.html
22222238[ SendChannel.close ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/close.html
22232239[ produce ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/produce.html
22242240[ consumeEach ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/consume-each.html
2225- [ Channel.invoke ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel/invoke.html
22262241[ actor ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/actor.html
22272242<!-- - INDEX kotlinx.coroutines.experimental.selects -->
22282243[ select ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.selects/select.html
0 commit comments