Skip to content

Commit 52a0ec0

Browse files
Sahil Loneelizarov
Sahil Lone
authored andcommitted
Fix guide for "Lazily started async"
See PR #442
1 parent 705ba56 commit 52a0ec0

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

core/kotlinx-coroutines-core/test/guide/example-compose-03.kt

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ fun main(args: Array<String>) = runBlocking<Unit> {
2222
val time = measureTimeMillis {
2323
val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
2424
val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
25+
// some computation
26+
one.start() // start the first one
27+
two.start() // start the second one
2528
println("The answer is ${one.await() + two.await()}")
2629
}
2730
println("Completed in $time ms")

core/kotlinx-coroutines-core/test/guide/test/GuideTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class GuideTest {
161161
fun testKotlinxCoroutinesExperimentalGuideCompose03() {
162162
test("KotlinxCoroutinesExperimentalGuideCompose03") { kotlinx.coroutines.experimental.guide.compose03.main(emptyArray()) }.verifyLinesArbitraryTime(
163163
"The answer is 42",
164-
"Completed in 2017 ms"
164+
"Completed in 1017 ms"
165165
)
166166
}
167167

coroutines-guide.md

+14-5
Original file line numberDiff line numberDiff line change
@@ -698,13 +698,16 @@ Note, that concurrency with coroutines is always explicit.
698698
There is a laziness option to [async] using an optional `start` parameter with a value of [CoroutineStart.LAZY].
699699
It starts coroutine only when its result is needed by some
700700
[await][Deferred.await] or if a [start][Job.start] function
701-
is invoked. Run the following example that differs from the previous one only by this option:
701+
is invoked. Run the following example:
702702

703703
```kotlin
704704
fun main(args: Array<String>) = runBlocking<Unit> {
705705
val time = measureTimeMillis {
706706
val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
707707
val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
708+
// some computation
709+
one.start() // start the first one
710+
two.start() // start the second one
708711
println("The answer is ${one.await() + two.await()}")
709712
}
710713
println("Completed in $time ms")
@@ -717,14 +720,20 @@ It produces something like this:
717720

718721
```text
719722
The answer is 42
720-
Completed in 2017 ms
723+
Completed in 1017 ms
721724
```
722725

723726
<!--- TEST ARBITRARY_TIME -->
724727

725-
So, we are back to sequential execution, because we _first_ start and await for `one`, _and then_ start and await
726-
for `two`. It is not the intended use-case for laziness. It is designed as a replacement for
727-
the standard `lazy` function in cases when computation of the value involves suspending functions.
728+
So, here the two coroutines are defined but not executed as in the previous example, but the control is given to
729+
the programmer about when exactly to start the execution by calling [start][Job.start] on it. We first
730+
start `one`, then start `two`, and then await for the individual coroutines to finish.
731+
732+
Note, that if we have called [await][Deferred.await] in `println` and omitted [start][Job.start] on individual
733+
coroutines, then we would have got the sequential behaviour as [await][Deferred.await] starts the coroutine
734+
execution and waits for the execution to finish, which is not the intended use-case for laziness.
735+
The use-case for `async(start = CoroutineStart.LAZY)` is a replacement for the
736+
standard `lazy` function in cases when computation of the value involves suspending functions.
728737

729738
### Async-style functions
730739

0 commit comments

Comments
 (0)