@@ -343,7 +343,8 @@ main: Now I can quit.
343
343
<!-- - TEST -->
344
344
345
345
As soon as main invokes ` job.cancel ` , we don't see any output from the other coroutine because it was cancelled.
346
- There is also an extension function [ cancelAndJoin] that combines [ cancel] and [ join] invocations.
346
+ There is also a [ Job] extension function [ cancelAndJoin]
347
+ that combines [ cancel] [ Job.cancel ] and [ join] [ Job.join ] invocations.
347
348
348
349
### Cancellation is cooperative
349
350
@@ -458,8 +459,8 @@ fun main(args: Array<String>) = runBlocking<Unit> {
458
459
459
460
> You can get full code [ here] ( core/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-04.kt )
460
461
461
- Both [ join] and [ cancelAndJoin] wait for all the finalization actions to complete, so the example above
462
- produces the following output:
462
+ Both [ join] [ Job.join ] and [ cancelAndJoin] wait for all the finalization actions to complete,
463
+ so the example above produces the following output:
463
464
464
465
``` text
465
466
I'm sleeping 0 ...
@@ -1047,7 +1048,7 @@ main: Who has survived request cancellation?
1047
1048
### Parental responsibilities
1048
1049
1049
1050
A parent coroutine always waits for completion of all its children. Parent does not have to explicitly track
1050
- all the children it launches and it does not have to use [ join] to wait for them at the end:
1051
+ all the children it launches and it does not have to use [ Job. join] to wait for them at the end:
1051
1052
1052
1053
``` kotlin
1053
1054
fun main (args : Array <String >) = runBlocking<Unit > {
@@ -1132,7 +1133,7 @@ and update data, do animations, etc. All of these coroutines must be cancelled w
1132
1133
to avoid memory leaks.
1133
1134
1134
1135
We can manage a lifecycle of our coroutines by creating an instance of [ Job] that is tied to
1135
- the lifecycle of our activity. A job instance is created using [ ` Job() ` ] [ Job ] factory function
1136
+ the lifecycle of our activity. A job instance is created using [ Job()] factory function
1136
1137
as the following example shows. We need to make sure that all the coroutines are started
1137
1138
with this job in their context and then a single invocation of [ Job.cancel] terminates them all.
1138
1139
Moreover, [ Job.join] waits for all of them to complete, so we can also use [ cancelAndJoin] here in
@@ -1373,7 +1374,7 @@ The following example prints the first ten prime numbers,
1373
1374
running the whole pipeline in the context of the main thread. Since all the coroutines are launched as
1374
1375
children of the main [ runBlocking] coroutine in its [ coroutineContext] [ CoroutineScope.coroutineContext ] ,
1375
1376
we don't have to keep an explicit list of all the coroutine we have created.
1376
- We use [ CoroutineContext. cancelChildren] extension to cancel all the children coroutines.
1377
+ We use [ cancelChildren] extension function to cancel all the children coroutines.
1377
1378
1378
1379
``` kotlin
1379
1380
fun main (args : Array <String >) = runBlocking<Unit > {
@@ -1529,7 +1530,7 @@ The channels shown so far had no buffer. Unbuffered channels transfer elements w
1529
1530
meet each other (aka rendezvous). If send is invoked first, then it is suspended until receive is invoked,
1530
1531
if receive is invoked first, it is suspended until send is invoked.
1531
1532
1532
- Both [ ` Channel() ` ] [ Channel ] factory function and [ produce] builder take an optional ` capacity ` parameter to
1533
+ Both [ Channel()] factory function and [ produce] builder take an optional ` capacity ` parameter to
1533
1534
specify _ buffer size_ . Buffer allows senders to send multiple elements before suspending,
1534
1535
similar to the ` BlockingQueue ` with a specified capacity, which blocks when buffer is full.
1535
1536
@@ -1829,7 +1830,7 @@ that is never executed concurrently. In a blocking world you'd typically use `sy
1829
1830
Coroutine's alternative is called [ Mutex] . It has [ lock] [ Mutex.lock ] and [ unlock] [ Mutex.unlock ] functions to
1830
1831
delimit a critical section. The key difference is that ` Mutex.lock ` is a suspending function. It does not block a thread.
1831
1832
1832
- There is also [ Mutex. withLock] extension function that conveniently represents
1833
+ There is also [ withLock] extension function that conveniently represents
1833
1834
` mutex.lock(); try { ... } finally { mutex.unlock() } ` pattern:
1834
1835
1835
1836
``` kotlin
@@ -2292,6 +2293,8 @@ Channel was closed
2292
2293
[ runBlocking ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/run-blocking.html
2293
2294
[ Job ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/index.html
2294
2295
[ cancelAndJoin ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/cancel-and-join.html
2296
+ [ Job.cancel ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/cancel.html
2297
+ [ Job.join ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/join.html
2295
2298
[ CancellationException ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-cancellation-exception.html
2296
2299
[ yield ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/yield.html
2297
2300
[ CoroutineScope.isActive ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope/is-active.html
@@ -2311,21 +2314,23 @@ Channel was closed
2311
2314
[ Unconfined ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-unconfined/index.html
2312
2315
[ newCoroutineContext ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-coroutine-context.html
2313
2316
[ CoroutineName ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-name/index.html
2314
- [ Job.cancel ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/cancel .html
2315
- [ Job.join ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/join .html
2317
+ [ Job() ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job.html
2318
+ [ cancelChildren ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/kotlin.coroutines.experimental.-coroutine-context/cancel-children .html
2316
2319
[ CompletableDeferred ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-completable-deferred/index.html
2317
2320
[ Deferred.onAwait ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-deferred/on-await.html
2318
2321
<!-- - INDEX kotlinx.coroutines.experimental.sync -->
2319
2322
[ Mutex ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/index.html
2320
2323
[ Mutex.lock ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/lock.html
2321
2324
[ Mutex.unlock ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/unlock.html
2325
+ [ withLock ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/with-lock.html
2322
2326
<!-- - INDEX kotlinx.coroutines.experimental.channels -->
2323
2327
[ Channel ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel/index.html
2324
2328
[ SendChannel.send ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/send.html
2325
2329
[ ReceiveChannel.receive ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/receive.html
2326
2330
[ SendChannel.close ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/close.html
2327
2331
[ produce ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/produce.html
2328
2332
[ consumeEach ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/consume-each.html
2333
+ [ Channel() ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel.html
2329
2334
[ actor ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/actor.html
2330
2335
[ ReceiveChannel.onReceive ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/on-receive.html
2331
2336
[ ReceiveChannel.onReceiveOrNull ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/on-receive-or-null.html
0 commit comments