Skip to content

Commit b65dc7f

Browse files
committed
More idiomatic and readable example for coroutines debug module readme
* Idiomatically use suspend functions * Idiomatic parallal decomposition with coroutineContext/async * No GlobalScope * Use less vertical space & more concise code to aid readability * Link to full code file (in tests)
1 parent 245a88e commit b65dc7f

File tree

2 files changed

+64
-48
lines changed

2 files changed

+64
-48
lines changed

core/kotlinx-coroutines-debug/README.md

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,86 +31,70 @@ Additionally, on Linux and Mac OS X you can use `kill -5 $pid` command in order
3131

3232
### Example of usage
3333

34-
Capabilities of this module can be demonstrated by the following example:
35-
```kotlin
36-
class Computation {
37-
public fun computeValue(): Deferred<String> = GlobalScope.async {
38-
val firstPart = computeFirstPart()
39-
val secondPart = computeSecondPart()
40-
41-
combineResults(firstPart, secondPart)
42-
}
34+
Capabilities of this module can be demonstrated by the following example
35+
(runnable code is [here](test/Example.kt)):
4336

44-
private suspend fun combineResults(firstPart: Deferred<String>, secondPart: Deferred<String>): String {
45-
return firstPart.await() + secondPart.await()
46-
}
37+
```kotlin
38+
suspend fun computeValue(): String = coroutineScope {
39+
val one = async { computeOne() }
40+
val two = async { computeTwo() }
41+
combineResults(one, two)
42+
}
4743

44+
suspend fun combineResults(one: Deferred<String>, two: Deferred<String>): String =
45+
one.await() + two.await()
4846

49-
private suspend fun CoroutineScope.computeFirstPart() = async {
50-
delay(5000)
51-
"4"
52-
}
47+
suspend fun computeOne(): String {
48+
delay(5000)
49+
return "4"
50+
}
5351

54-
private suspend fun CoroutineScope.computeSecondPart() = async {
55-
delay(5000)
56-
"2"
57-
}
52+
suspend fun computeTwo(): String {
53+
delay(5000)
54+
return "2"
5855
}
5956

60-
fun main(args: Array<String>) = runBlocking {
57+
fun main() = runBlocking {
6158
DebugProbes.install()
62-
val computation = Computation()
63-
val deferred = computation.computeValue()
64-
59+
val deferred = async { computeValue() }
6560
// Delay for some time
6661
delay(1000)
67-
62+
// Dump running coroutines
6863
DebugProbes.dumpCoroutines()
69-
7064
println("\nDumping only deferred")
7165
DebugProbes.printJob(deferred)
7266
}
7367
```
7468

7569
Printed result will be:
70+
7671
```
7772
Coroutines dump 2018/11/12 21:44:02
7873
79-
Coroutine "coroutine#2":DeferredCoroutine{Active}@1b26f7b2, state: SUSPENDED
74+
Coroutine "coroutine#2":DeferredCoroutine{Active}@289d1c02, state: SUSPENDED
8075
at kotlinx.coroutines.DeferredCoroutine.await$suspendImpl(Builders.common.kt:99)
81-
at Computation.combineResults(Example.kt:18)
82-
at Computation$computeValue$1.invokeSuspend(Example.kt:14)
76+
at ExampleKt.combineResults(Example.kt:11)
77+
at ExampleKt$computeValue$2.invokeSuspend(Example.kt:7)
78+
at ExampleKt$main$1$deferred$1.invokeSuspend(Example.kt:25)
8379
(Coroutine creation stacktrace)
8480
at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt:116)
85-
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:23)
86-
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
87-
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:160)
88-
at kotlinx.coroutines.BuildersKt__Builders_commonKt.async(Builders.common.kt:88)
89-
at kotlinx.coroutines.BuildersKt.async(Unknown Source)
90-
at kotlinx.coroutines.BuildersKt__Builders_commonKt.async$default(Builders.common.kt:81)
81+
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:25)
9182
at kotlinx.coroutines.BuildersKt.async$default(Unknown Source)
92-
at Computation.computeValue(Example.kt:10)
93-
at ExampleKt$main$1.invokeSuspend(Example.kt:36)
83+
at ExampleKt$main$1.invokeSuspend(Example.kt:25)
9484
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
95-
at kotlinx.coroutines.DispatchedTask$DefaultImpls.run(Dispatched.kt:237)
96-
at kotlinx.coroutines.DispatchedContinuation.run(Dispatched.kt:81)
97-
at kotlinx.coroutines.EventLoopBase.processNextEvent(EventLoop.kt:123)
98-
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:69)
99-
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:45)
100-
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
101-
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:35)
85+
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
10286
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
103-
at ExampleKt.main(Example.kt:33)
87+
at ExampleKt.main(Example.kt:23)
88+
at ExampleKt.main(Example.kt)
10489
10590
... More coroutines here ...
10691
10792
Dumping only deferred
10893
"coroutine#2":DeferredCoroutine{Active}, continuation is SUSPENDED at line kotlinx.coroutines.DeferredCoroutine.await$suspendImpl(Builders.common.kt:99)
109-
"coroutine#3":DeferredCoroutine{Active}, continuation is SUSPENDED at line Computation$computeFirstPart$2.invokeSuspend(Example.kt:23)
110-
"coroutine#4":DeferredCoroutine{Active}, continuation is SUSPENDED at line Computation$computeSecondPart$2.invokeSuspend(Example.kt:28)
94+
"coroutine#3":DeferredCoroutine{Active}, continuation is SUSPENDED at line ExampleKt.computeOne(Example.kt:14)
95+
"coroutine#4":DeferredCoroutine{Active}, continuation is SUSPENDED at line ExampleKt.computeTwo(Example.kt:19)
11196
```
11297

113-
11498
### Status of the API
11599

116100
API is purely experimental and it is not guaranteed that it won't be changed (while it is marked as `@ExperimentalCoroutinesApi`).
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import kotlinx.coroutines.*
2+
import kotlinx.coroutines.debug.*
3+
4+
suspend fun computeValue(): String = coroutineScope {
5+
val one = async { computeOne() }
6+
val two = async { computeTwo() }
7+
combineResults(one, two)
8+
}
9+
10+
suspend fun combineResults(one: Deferred<String>, two: Deferred<String>): String =
11+
one.await() + two.await()
12+
13+
suspend fun computeOne(): String {
14+
delay(5000)
15+
return "4"
16+
}
17+
18+
suspend fun computeTwo(): String {
19+
delay(5000)
20+
return "2"
21+
}
22+
23+
fun main() = runBlocking {
24+
DebugProbes.install()
25+
val deferred = async { computeValue() }
26+
// Delay for some time
27+
delay(1000)
28+
// Dump running coroutines
29+
DebugProbes.dumpCoroutines()
30+
println("\nDumping only deferred")
31+
DebugProbes.printJob(deferred)
32+
}

0 commit comments

Comments
 (0)