@@ -22,7 +22,16 @@ This is a short guide on core features of `kotlinx.coroutines` with a series of
2222 * [ Sequential by default] ( #sequential-by-default )
2323 * [ Concurrent using deferred value] ( #concurrent-using-deferred-value )
2424 * [ Lazily deferred value] ( #lazily-deferred-value )
25+
26+ <!-- - KNIT kotlinx-coroutines-core/src/test/kotlin/guide/.*\.kt -->
2527
28+ <!-- - INCLUDE .*/example-([0-9]+)\.kt
29+ // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
30+ package guide.example$$1
31+
32+ import kotlinx.coroutines.experimental.*
33+ -->
34+
2635## Coroutine basics
2736
2837This section covers basic coroutine concepts.
@@ -42,7 +51,7 @@ fun main(args: Array<String>) {
4251}
4352```
4453
45- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-11.kt )
54+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-11.kt )
4655
4756Run this code:
4857
@@ -80,7 +89,7 @@ fun main(args: Array<String>) = runBlocking<Unit> { // start main coroutine
8089}
8190```
8291
83- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-12.kt )
92+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-12.kt )
8493
8594The result is the same, but this code uses only non-blocking ` delay ` .
8695
@@ -97,6 +106,8 @@ class MyTest {
97106 }
98107}
99108```
109+
110+ <!-- - CLEAR -->
100111
101112### Waiting for a job
102113
@@ -114,7 +125,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
114125}
115126```
116127
117- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-13.kt )
128+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-13.kt )
118129
119130Now the result is still the same, but the code of the main coroutine is not tied to the duration of
120131the background job in any way. Much better.
@@ -141,7 +152,7 @@ suspend fun doWorld() {
141152}
142153```
143154
144- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-14.kt )
155+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-14.kt )
145156
146157### Coroutines ARE light-weight
147158
@@ -159,7 +170,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
159170}
160171```
161172
162- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-15.kt )
173+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-15.kt )
163174
164175It starts 100K coroutines and, after a second, each coroutine prints a dot.
165176Now, try that with threads. What would happen? (Most likely your code will produce some sort of out-of-memory error)
@@ -181,7 +192,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
181192}
182193```
183194
184- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-16.kt )
195+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-16.kt )
185196
186197You can run and see that it prints three lines and terminates:
187198
@@ -219,7 +230,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
219230}
220231```
221232
222- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-21.kt )
233+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-21.kt )
223234
224235It produces the following output:
225236
@@ -262,7 +273,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
262273}
263274```
264275
265- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-22.kt )
276+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-22.kt )
266277
267278Run it to see that it continues to print "I'm sleeping" even after cancellation.
268279
@@ -274,7 +285,28 @@ The other one is to explicitly check the cancellation status. Let us try the lat
274285
275286Replace ` while (true) ` in the previous example with ` while (isActive) ` and rerun it.
276287
277- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples/example-23.kt )
288+ ``` kotlin
289+ fun main (args : Array <String >) = runBlocking<Unit > {
290+ val job = launch(CommonPool ) {
291+ var nextPrintTime = 0L
292+ var i = 0
293+ while (isActive) { // cancellable computation loop
294+ val currentTime = System .currentTimeMillis()
295+ if (currentTime >= nextPrintTime) {
296+ println (" I'm sleeping ${i++ } ..." )
297+ nextPrintTime = currentTime + 500L
298+ }
299+ }
300+ }
301+ delay(1300L ) // delay a bit
302+ println (" main: I'm tired of waiting!" )
303+ job.cancel() // cancels the job
304+ delay(1300L ) // delay a bit to see if it was cancelled....
305+ println (" main: Now I can quit." )
306+ }
307+ ```
308+
309+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide/example-23.kt )
278310
279311As you can see, now this loop can be cancelled. ` isActive ` is a property that is available inside
280312the code of coroutines via ` CoroutineScope ` object.
@@ -305,7 +337,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
305337}
306338```
307339
308- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-24.kt )
340+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-24.kt )
309341
310342The example above produces the following output:
311343
@@ -351,7 +383,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
351383}
352384```
353385
354- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-25.kt )
386+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-25.kt )
355387
356388### Timeout
357389
@@ -372,7 +404,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
372404}
373405```
374406
375- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-26.kt )
407+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-26.kt )
376408
377409It produces the following output:
378410
@@ -422,6 +454,10 @@ We just use a normal sequential invocation, because the code in the coroutine, j
422454code, is _ sequential_ by default. The following example demonstrates that by measuring the total
423455time it takes to execute both suspending functions:
424456
457+ <!-- - INCLUDE .*/example-3[1-9].kt
458+ import kotlin.system.measureTimeMillis
459+ -->
460+
425461``` kotlin
426462fun main (args : Array <String >) = runBlocking<Unit > {
427463 val time = measureTimeMillis {
@@ -433,7 +469,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
433469}
434470```
435471
436- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-31.kt )
472+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-31.kt )
437473
438474It produces something like this:
439475
@@ -453,6 +489,19 @@ does not carry any resulting value, while `defer` returns a `Deferred` -- a kind
453489that represent a promise to provide result later. You can use ` .await() ` on a deferred value to get its eventual result,
454490but ` Deferred ` is also a ` Job ` , so you can cancel it if needed.
455491
492+ <!-- - INCLUDE .*/example-3[2-9].kt
493+
494+ suspend fun doSomethingUsefulOne(): Int {
495+ delay(1000L) // pretend we are doing something useful here
496+ return 13
497+ }
498+
499+ suspend fun doSomethingUsefulTwo(): Int {
500+ delay(1000L) // pretend we are doing something useful here, too
501+ return 29
502+ }
503+ -->
504+
456505``` kotlin
457506fun main (args : Array <String >) = runBlocking<Unit > {
458507 val time = measureTimeMillis {
@@ -464,7 +513,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
464513}
465514```
466515
467- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-32.kt )
516+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-32.kt )
468517
469518It produces something like this:
470519
@@ -493,7 +542,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
493542}
494543```
495544
496- > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/examples /example-33.kt )
545+ > You can get full code [ here] ( kotlinx-coroutines-core/src/test/kotlin/guide /example-33.kt )
497546
498547It produces something like this:
499548
0 commit comments