@@ -4,6 +4,8 @@ This is a short guide on core features of `kotlinx.coroutines` with a series of
44
55## Table of contents
66
7+ <!-- - TOC -->
8+
79* [ Coroutine basics] ( #coroutine-basics )
810 * [ Your first coroutine] ( #your-first-coroutine )
911 * [ Bridging blocking and non-blocking worlds] ( #bridging-blocking-and-non-blocking-worlds )
@@ -23,20 +25,20 @@ This is a short guide on core features of `kotlinx.coroutines` with a series of
2325 * [ Concurrent using deferred value] ( #concurrent-using-deferred-value )
2426 * [ Lazily deferred value] ( #lazily-deferred-value )
2527* [ Coroutine context and dispatchers] ( #coroutine-context-and-dispatchers )
26- * [ Dispatchers and threads] ( #Dispatchers -and-threads )
28+ * [ Dispatchers and threads] ( #dispatchers -and-threads )
2729 * [ Unconfined vs confined dispatcher] ( #unconfined-vs-confined-dispatcher )
2830 * [ Debugging coroutines and threads] ( #debugging-coroutines-and-threads )
2931 * [ Jumping between threads] ( #jumping-between-threads )
3032 * [ Job in the context] ( #job-in-the-context )
3133 * [ Children of a coroutine] ( #children-of-a-coroutine )
3234 * [ Combining contexts] ( #combining-contexts )
3335 * [ Naming coroutines for debugging] ( #naming-coroutines-for-debugging )
34-
36+
3537<!-- - KNIT kotlinx-coroutines-core/src/test/kotlin/guide/.*\.kt -->
3638
37- <!-- - INCLUDE .*/example-([0-9]+)\.kt
39+ <!-- - INCLUDE .*/example-([a-z]+)-([ 0-9]+)\.kt
3840// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
39- package guide.example $$1
41+ package guide.$$1.example$$2
4042
4143import kotlinx.coroutines.experimental.*
4244-->
@@ -60,7 +62,7 @@ fun main(args: Array<String>) {
6062}
6163```
6264
63- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-11 .kt )
65+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-01 .kt )
6466
6567Run this code:
6668
@@ -98,7 +100,7 @@ fun main(args: Array<String>) = runBlocking<Unit> { // start main coroutine
98100}
99101```
100102
101- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-12 .kt )
103+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-02 .kt )
102104
103105The result is the same, but this code uses only non-blocking ` delay ` .
104106
@@ -134,7 +136,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
134136}
135137```
136138
137- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-13 .kt )
139+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-03 .kt )
138140
139141Now the result is still the same, but the code of the main coroutine is not tied to the duration of
140142the background job in any way. Much better.
@@ -161,7 +163,7 @@ suspend fun doWorld() {
161163}
162164```
163165
164- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-14 .kt )
166+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-04 .kt )
165167
166168### Coroutines ARE light-weight
167169
@@ -179,7 +181,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
179181}
180182```
181183
182- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-15 .kt )
184+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-05 .kt )
183185
184186It starts 100K coroutines and, after a second, each coroutine prints a dot.
185187Now, try that with threads. What would happen? (Most likely your code will produce some sort of out-of-memory error)
@@ -201,7 +203,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
201203}
202204```
203205
204- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-16 .kt )
206+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-06 .kt )
205207
206208You can run and see that it prints three lines and terminates:
207209
@@ -239,7 +241,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
239241}
240242```
241243
242- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-21 .kt )
244+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-01 .kt )
243245
244246It produces the following output:
245247
@@ -282,7 +284,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
282284}
283285```
284286
285- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-22 .kt )
287+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-02 .kt )
286288
287289Run it to see that it continues to print "I'm sleeping" even after cancellation.
288290
@@ -315,7 +317,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
315317}
316318```
317319
318- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-23 .kt )
320+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-03 .kt )
319321
320322As you can see, now this loop can be cancelled. ` isActive ` is a property that is available inside
321323the code of coroutines via ` CoroutineScope ` object.
@@ -346,7 +348,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
346348}
347349```
348350
349- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-24 .kt )
351+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-04 .kt )
350352
351353The example above produces the following output:
352354
@@ -392,7 +394,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
392394}
393395```
394396
395- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-25 .kt )
397+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-05 .kt )
396398
397399### Timeout
398400
@@ -413,7 +415,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
413415}
414416```
415417
416- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-26 .kt )
418+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-06 .kt )
417419
418420It produces the following output:
419421
@@ -442,6 +444,10 @@ Assume that we have two suspending functions defined elsewhere that do something
442444remote service call or computation. We'll just pretend they are useful, but each one will just actaully
443445delay for a second for the purpose of this example:
444446
447+ <!-- - INCLUDE .*/example-compose-([0-9]+).kt
448+ import kotlin.system.measureTimeMillis
449+ -->
450+
445451``` kotlin
446452suspend fun doSomethingUsefulOne (): Int {
447453 delay(1000L ) // pretend we are doing something useful here
@@ -454,6 +460,8 @@ suspend fun doSomethingUsefulTwo(): Int {
454460}
455461```
456462
463+ <!-- - INCLUDE .*/example-compose-([0-9]+).kt -->
464+
457465What do we do if need to invoke them _ sequentially_ -- first ` doSomethingUsefulOne ` _ and then_
458466` doSomethingUsefulTwo ` and compute the sum of their results?
459467In practise we do this if we use the results of the first function to make a decision on whether we need
@@ -463,10 +471,6 @@ We just use a normal sequential invocation, because the code in the coroutine, j
463471code, is _ sequential_ by default. The following example demonstrates that by measuring the total
464472time it takes to execute both suspending functions:
465473
466- <!-- - INCLUDE .*/example-3[1-9].kt
467- import kotlin.system.measureTimeMillis
468- -->
469-
470474``` kotlin
471475fun main (args : Array <String >) = runBlocking<Unit > {
472476 val time = measureTimeMillis {
@@ -478,7 +482,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
478482}
479483```
480484
481- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-31 .kt )
485+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-01 .kt )
482486
483487It produces something like this:
484488
@@ -498,19 +502,6 @@ does not carry any resulting value, while `defer` returns a `Deferred` -- a kind
498502that represent a promise to provide result later. You can use ` .await() ` on a deferred value to get its eventual result,
499503but ` Deferred ` is also a ` Job ` , so you can cancel it if needed.
500504
501- <!-- - INCLUDE .*/example-3[2-9].kt
502-
503- suspend fun doSomethingUsefulOne(): Int {
504- delay(1000L) // pretend we are doing something useful here
505- return 13
506- }
507-
508- suspend fun doSomethingUsefulTwo(): Int {
509- delay(1000L) // pretend we are doing something useful here, too
510- return 29
511- }
512- -->
513-
514505``` kotlin
515506fun main (args : Array <String >) = runBlocking<Unit > {
516507 val time = measureTimeMillis {
@@ -522,7 +513,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
522513}
523514```
524515
525- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-32 .kt )
516+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-02 .kt )
526517
527518It produces something like this:
528519
@@ -551,7 +542,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
551542}
552543```
553544
554- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-33 .kt )
545+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-03 .kt )
555546
556547It produces something like this:
557548
@@ -595,7 +586,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
595586}
596587```
597588
598- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-41 .kt )
589+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-context-01 .kt )
599590
600591It produces the following output (maybe in different order):
601592
@@ -638,7 +629,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
638629}
639630```
640631
641- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-42 .kt )
632+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-contest-02 .kt )
642633
643634Produces the output:
644635
@@ -679,7 +670,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
679670}
680671```
681672
682- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-43 .kt )
673+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-context-03 .kt )
683674
684675There are three coroutines. The main couroutine (#1 ) -- ` runBlocking ` one,
685676and two coroutines computing deferred values ` a ` (#2 ) and ` b ` (#3 ).
@@ -718,7 +709,7 @@ fun main(args: Array<String>) {
718709}
719710```
720711
721- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-44 .kt )
712+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-context-04 .kt )
722713
723714It demonstrates two new techniques. One is using ` runBlocking ` with an explicitly specified context, and
724715the second one is using ` run(context) {...} ` to change a context of a coroutine while still staying in the
@@ -741,7 +732,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
741732}
742733```
743734
744- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-45 .kt )
735+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-context-05 .kt )
745736
746737It produces
747738
@@ -784,7 +775,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
784775}
785776```
786777
787- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-46 .kt )
778+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-context-06 .kt )
788779
789780The output of this code is:
790781
@@ -820,7 +811,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
820811}
821812```
822813
823- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-47 .kt )
814+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-context-07 .kt )
824815
825816The expected outcome of this code is:
826817
@@ -859,7 +850,7 @@ fun main(args: Array<String>) = runBlocking(CoroutineName("main")) {
859850}
860851```
861852
862- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-48 .kt )
853+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-context-08 .kt )
863854
864855The output it produces with ` -Dkotlinx.coroutines.debug ` JVM option is similar to:
865856
0 commit comments