@@ -22,7 +22,16 @@ This is a short guide on core features of `kotlinx.coroutines` with a series of
22
22
* [ Sequential by default] ( #sequential-by-default )
23
23
* [ Concurrent using deferred value] ( #concurrent-using-deferred-value )
24
24
* [ Lazily deferred value] ( #lazily-deferred-value )
25
+
26
+ <!-- - KNIT kotlinx-coroutines-core/src/test/kotlin/guide/.*\.kt -->
25
27
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
+
26
35
## Coroutine basics
27
36
28
37
This section covers basic coroutine concepts.
@@ -42,7 +51,7 @@ fun main(args: Array<String>) {
42
51
}
43
52
```
44
53
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 )
46
55
47
56
Run this code:
48
57
@@ -80,7 +89,7 @@ fun main(args: Array<String>) = runBlocking<Unit> { // start main coroutine
80
89
}
81
90
```
82
91
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 )
84
93
85
94
The result is the same, but this code uses only non-blocking ` delay ` .
86
95
@@ -97,6 +106,8 @@ class MyTest {
97
106
}
98
107
}
99
108
```
109
+
110
+ <!-- - CLEAR -->
100
111
101
112
### Waiting for a job
102
113
@@ -114,7 +125,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
114
125
}
115
126
```
116
127
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 )
118
129
119
130
Now the result is still the same, but the code of the main coroutine is not tied to the duration of
120
131
the background job in any way. Much better.
@@ -141,7 +152,7 @@ suspend fun doWorld() {
141
152
}
142
153
```
143
154
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 )
145
156
146
157
### Coroutines ARE light-weight
147
158
@@ -159,7 +170,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
159
170
}
160
171
```
161
172
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 )
163
174
164
175
It starts 100K coroutines and, after a second, each coroutine prints a dot.
165
176
Now, 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> {
181
192
}
182
193
```
183
194
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 )
185
196
186
197
You can run and see that it prints three lines and terminates:
187
198
@@ -219,7 +230,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
219
230
}
220
231
```
221
232
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 )
223
234
224
235
It produces the following output:
225
236
@@ -262,7 +273,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
262
273
}
263
274
```
264
275
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 )
266
277
267
278
Run it to see that it continues to print "I'm sleeping" even after cancellation.
268
279
@@ -274,7 +285,28 @@ The other one is to explicitly check the cancellation status. Let us try the lat
274
285
275
286
Replace ` while (true) ` in the previous example with ` while (isActive) ` and rerun it.
276
287
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 )
278
310
279
311
As you can see, now this loop can be cancelled. ` isActive ` is a property that is available inside
280
312
the code of coroutines via ` CoroutineScope ` object.
@@ -305,7 +337,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
305
337
}
306
338
```
307
339
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 )
309
341
310
342
The example above produces the following output:
311
343
@@ -351,7 +383,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
351
383
}
352
384
```
353
385
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 )
355
387
356
388
### Timeout
357
389
@@ -372,7 +404,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
372
404
}
373
405
```
374
406
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 )
376
408
377
409
It produces the following output:
378
410
@@ -422,6 +454,10 @@ We just use a normal sequential invocation, because the code in the coroutine, j
422
454
code, is _ sequential_ by default. The following example demonstrates that by measuring the total
423
455
time it takes to execute both suspending functions:
424
456
457
+ <!-- - INCLUDE .*/example-3[1-9].kt
458
+ import kotlin.system.measureTimeMillis
459
+ -->
460
+
425
461
``` kotlin
426
462
fun main (args : Array <String >) = runBlocking<Unit > {
427
463
val time = measureTimeMillis {
@@ -433,7 +469,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
433
469
}
434
470
```
435
471
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 )
437
473
438
474
It produces something like this:
439
475
@@ -453,6 +489,19 @@ does not carry any resulting value, while `defer` returns a `Deferred` -- a kind
453
489
that represent a promise to provide result later. You can use ` .await() ` on a deferred value to get its eventual result,
454
490
but ` Deferred ` is also a ` Job ` , so you can cancel it if needed.
455
491
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
+
456
505
``` kotlin
457
506
fun main (args : Array <String >) = runBlocking<Unit > {
458
507
val time = measureTimeMillis {
@@ -464,7 +513,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
464
513
}
465
514
```
466
515
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 )
468
517
469
518
It produces something like this:
470
519
@@ -493,7 +542,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
493
542
}
494
543
```
495
544
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 )
497
546
498
547
It produces something like this:
499
548
0 commit comments