@@ -1068,7 +1068,7 @@ and update data, do animations, etc. All of these coroutines must be cancelled w
1068
1068
to avoid memory leaks.
1069
1069
1070
1070
We 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
1072
1072
as the following example shows. We need to make sure that all the coroutines are started
1073
1073
with this job in their context and then a single invocation of [ Job.cancel] terminates them all.
1074
1074
@@ -1458,7 +1458,7 @@ The channels shown so far had no buffer. Unbuffered channels transfer elements w
1458
1458
meet each other (aka rendezvous). If send is invoked first, then it is suspended until receive is invoked,
1459
1459
if receive is invoked first, it is suspended until send is invoked.
1460
1460
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
1462
1462
specify _ buffer size_ . Buffer allows senders to send multiple elements before suspending,
1463
1463
similar to the ` BlockingQueue ` with a specified capacity, which blocks when buffer is full.
1464
1464
@@ -1790,31 +1790,47 @@ There is an [actor] coroutine builder that conveniently combines actor's mailbox
1790
1790
scope to receive messages from and combines the send channel into the resulting job object, so that a
1791
1791
single reference to the actor can be carried around as its handle.
1792
1792
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
+
1793
1800
``` kotlin
1794
1801
// Message types for counterActor
1795
1802
sealed class CounterMsg
1796
1803
object 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
+ ```
1798
1806
1807
+ Then we define a function that launches an actor using an [ actor] coroutine builder:
1808
+
1809
+ ``` kotlin
1799
1810
// This function launches a new counter actor
1800
1811
fun counterActor () = actor<CounterMsg >(CommonPool ) {
1801
1812
var counter = 0 // actor state
1802
1813
for (msg in channel) { // iterate over incoming messages
1803
1814
when (msg) {
1804
1815
is IncCounter -> counter++
1805
- is GetCounter -> msg.response.send (counter)
1816
+ is GetCounter -> msg.response.complete (counter)
1806
1817
}
1807
1818
}
1808
1819
}
1820
+ ```
1821
+
1822
+ The main code is straightforward:
1809
1823
1824
+ ``` kotlin
1810
1825
fun main (args : Array <String >) = runBlocking<Unit > {
1811
1826
val counter = counterActor() // create the actor
1812
1827
massiveRun(CommonPool ) {
1813
1828
counter.send(IncCounter )
1814
1829
}
1815
- val response = Channel <Int >()
1830
+ // send a message to get a counter value from an actor
1831
+ val response = CompletableDeferred <Int >()
1816
1832
counter.send(GetCounter (response))
1817
- println (" Counter = ${response.receive ()} " )
1833
+ println (" Counter = ${response.await ()} " )
1818
1834
counter.close() // shutdown the actor
1819
1835
}
1820
1836
```
@@ -2190,7 +2206,7 @@ Channel was closed
2190
2206
[ launch ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/launch.html
2191
2207
[ delay ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/delay.html
2192
2208
[ 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
2194
2210
[ CancellationException ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-cancellation-exception.html
2195
2211
[ yield ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/yield.html
2196
2212
[ 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
2209
2225
[ Unconfined ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-unconfined/index.html
2210
2226
[ newCoroutineContext ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-coroutine-context.html
2211
2227
[ 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
2213
2228
[ 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
2214
2230
<!-- - 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
2216
2232
[ Mutex.lock ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/lock.html
2217
2233
[ Mutex.unlock ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/unlock.html
2218
2234
<!-- - 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
2220
2236
[ SendChannel.send ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/send.html
2221
2237
[ ReceiveChannel.receive ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/receive.html
2222
2238
[ SendChannel.close ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/close.html
2223
2239
[ produce ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/produce.html
2224
2240
[ 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
2226
2241
[ actor ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/actor.html
2227
2242
<!-- - INDEX kotlinx.coroutines.experimental.selects -->
2228
2243
[ select ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.selects/select.html
0 commit comments