Skip to content

Commit 256812a

Browse files
committed
Use CompletableDeferred in the guide section on coroutines;
fix (reknit) broken links to factory functions that are now top-level
1 parent 8b9ecff commit 256812a

File tree

5 files changed

+45
-26
lines changed

5 files changed

+45
-26
lines changed

coroutines-guide.md

+26-11
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ and update data, do animations, etc. All of these coroutines must be cancelled w
10681068
to avoid memory leaks.
10691069

10701070
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
10721072
as the following example shows. We need to make sure that all the coroutines are started
10731073
with 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
14581458
meet each other (aka rendezvous). If send is invoked first, then it is suspended until receive is invoked,
14591459
if 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
14621462
specify _buffer size_. Buffer allows senders to send multiple elements before suspending,
14631463
similar 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
17901790
scope to receive messages from and combines the send channel into the resulting job object, so that a
17911791
single 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
17951802
sealed class CounterMsg
17961803
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+
```
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
18001811
fun 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
18101825
fun 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

kotlinx-coroutines-core/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Select expression to perform multiple suspending operations simultaneously until
8585
<!--- MODULE kotlinx-coroutines-core -->
8686
<!--- INDEX kotlinx.coroutines.experimental -->
8787
[launch]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/launch.html
88-
[Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/index.html
88+
[Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job.html
8989
[CoroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope/index.html
9090
[async]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/async.html
9191
[Deferred]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-deferred/index.html
@@ -97,7 +97,7 @@ Select expression to perform multiple suspending operations simultaneously until
9797
[java.util.concurrent.Executor.asCoroutineDispatcher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/java.util.concurrent.-executor/as-coroutine-dispatcher.html
9898
[Unconfined]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-unconfined/index.html
9999
[NonCancellable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-non-cancellable/index.html
100-
[CoroutineExceptionHandler]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-exception-handler/index.html
100+
[CoroutineExceptionHandler]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-exception-handler.html
101101
[delay]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/delay.html
102102
[yield]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/yield.html
103103
[run]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/run.html
@@ -109,7 +109,7 @@ Select expression to perform multiple suspending operations simultaneously until
109109
[suspendCancellableCoroutine]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/suspend-cancellable-coroutine.html
110110
[newCoroutineContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-coroutine-context.html
111111
<!--- INDEX kotlinx.coroutines.experimental.sync -->
112-
[kotlinx.coroutines.experimental.sync.Mutex]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/index.html
112+
[kotlinx.coroutines.experimental.sync.Mutex]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex.html
113113
[kotlinx.coroutines.experimental.sync.Mutex.lock]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/lock.html
114114
[kotlinx.coroutines.experimental.sync.Mutex.tryLock]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/try-lock.html
115115
<!--- INDEX kotlinx.coroutines.experimental.channels -->
@@ -119,7 +119,7 @@ Select expression to perform multiple suspending operations simultaneously until
119119
[kotlinx.coroutines.experimental.channels.actor]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/actor.html
120120
[kotlinx.coroutines.experimental.channels.ActorJob]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-actor-job/index.html
121121
[kotlinx.coroutines.experimental.channels.ActorScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-actor-scope/index.html
122-
[kotlinx.coroutines.experimental.channels.Channel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel/index.html
122+
[kotlinx.coroutines.experimental.channels.Channel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel.html
123123
[kotlinx.coroutines.experimental.channels.SendChannel.send]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/send.html
124124
[kotlinx.coroutines.experimental.channels.ReceiveChannel.receive]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/receive.html
125125
[kotlinx.coroutines.experimental.channels.SendChannel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/index.html

kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-07.kt

+10-6
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
1818
package guide.sync.example07
1919

20-
import kotlinx.coroutines.experimental.*
20+
import kotlinx.coroutines.experimental.CommonPool
21+
import kotlinx.coroutines.experimental.CompletableDeferred
22+
import kotlinx.coroutines.experimental.channels.actor
23+
import kotlinx.coroutines.experimental.launch
24+
import kotlinx.coroutines.experimental.runBlocking
2125
import kotlin.coroutines.experimental.CoroutineContext
2226
import kotlin.system.measureTimeMillis
23-
import kotlinx.coroutines.experimental.channels.*
2427

2528
suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) {
2629
val n = 1000 // number of coroutines to launch
@@ -39,15 +42,15 @@ suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) {
3942
// Message types for counterActor
4043
sealed class CounterMsg
4144
object IncCounter : CounterMsg() // one-way message to increment counter
42-
class GetCounter(val response: SendChannel<Int>) : CounterMsg() // a request with reply
45+
class GetCounter(val response: CompletableDeferred<Int>) : CounterMsg() // a request with reply
4346

4447
// This function launches a new counter actor
4548
fun counterActor() = actor<CounterMsg>(CommonPool) {
4649
var counter = 0 // actor state
4750
for (msg in channel) { // iterate over incoming messages
4851
when (msg) {
4952
is IncCounter -> counter++
50-
is GetCounter -> msg.response.send(counter)
53+
is GetCounter -> msg.response.complete(counter)
5154
}
5255
}
5356
}
@@ -57,8 +60,9 @@ fun main(args: Array<String>) = runBlocking<Unit> {
5760
massiveRun(CommonPool) {
5861
counter.send(IncCounter)
5962
}
60-
val response = Channel<Int>()
63+
// send a message to get a counter value from an actor
64+
val response = CompletableDeferred<Int>()
6165
counter.send(GetCounter(response))
62-
println("Counter = ${response.receive()}")
66+
println("Counter = ${response.await()}")
6367
counter.close() // shutdown the actor
6468
}

reactive/coroutines-guide-reactive.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1054,14 +1054,14 @@ coroutines for complex pipelines with fan-in and fan-out between multiple worker
10541054
[CommonPool]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-common-pool/index.html
10551055
[Job.join]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/join.html
10561056
<!--- INDEX kotlinx.coroutines.experimental.channels -->
1057-
[Channel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel/index.html
1057+
[Channel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel.html
10581058
[ReceiveChannel.receive]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/receive.html
10591059
[produce]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/produce.html
10601060
[consumeEach]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/consume-each.html
10611061
[ReceiveChannel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/index.html
10621062
[SubscriptionReceiveChannel.close]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-subscription-receive-channel/close.html
10631063
[SendChannel.send]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/send.html
1064-
[BroadcastChannel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-broadcast-channel/index.html
1064+
[BroadcastChannel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-broadcast-channel.html
10651065
[ConflatedBroadcastChannel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-conflated-broadcast-channel/index.html
10661066
[ArrayBroadcastChannel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-array-broadcast-channel/index.html
10671067
<!--- INDEX kotlinx.coroutines.experimental.selects -->

ui/coroutines-guide-ui.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ fun View.onClick(action: suspend () -> Unit) {
376376
Sometimes it is more appropriate to process the most recent event, instead of just ignoring events while we were busy
377377
processing the previous one. The [actor] coroutine builder accepts an optional `capacity` parameter that
378378
controls the implementation of the channel that this actor is using for its mailbox. The description of all
379-
the available choices is given in documentation of the [Channel()][Channel.invoke] factory function.
379+
the available choices is given in documentation of the [`Channel()`][Channel] factory function.
380380

381381
Let us change the code to use [ConflatedChannel] by passing [Channel.CONFLATED] capacity value. The
382382
change is only to the line that creates an actor:
@@ -698,7 +698,7 @@ After delay
698698
<!--- INDEX kotlinx.coroutines.experimental -->
699699
[launch]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/launch.html
700700
[delay]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/delay.html
701-
[Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/index.html
701+
[Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job.html
702702
[Job.cancel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/cancel.html
703703
[run]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/run.html
704704
[CommonPool]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-common-pool/index.html
@@ -711,7 +711,7 @@ After delay
711711
[SendChannel.offer]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/offer.html
712712
[SendChannel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/index.html
713713
[RendezvousChannel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-rendezvous-channel/index.html
714-
[Channel.invoke]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel/invoke.html
714+
[Channel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel.html
715715
[ConflatedChannel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-conflated-channel/index.html
716716
[Channel.CONFLATED]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel/-c-o-n-f-l-a-t-e-d.html
717717
[LinkedListChannel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-linked-list-channel/index.html

0 commit comments

Comments
 (0)