Skip to content

Commit 1293ccd

Browse files
committed
Coroutine guide: more on cancellation and "Composing suspending functions" section
1 parent 79a2868 commit 1293ccd

16 files changed

+451
-44
lines changed

coroutines-guide.md

Lines changed: 297 additions & 29 deletions
Large diffs are not rendered by default.

kotlinx-coroutines-core/src/test/kotlin/examples/example-01.kt renamed to kotlinx-coroutines-core/src/test/kotlin/examples/example-11.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package example11
22

33
import kotlinx.coroutines.experimental.CommonPool
44
import kotlinx.coroutines.experimental.delay

kotlinx-coroutines-core/src/test/kotlin/examples/example-02.kt renamed to kotlinx-coroutines-core/src/test/kotlin/examples/example-12.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package example12
22

33
import kotlinx.coroutines.experimental.CommonPool
44
import kotlinx.coroutines.experimental.delay

kotlinx-coroutines-core/src/test/kotlin/examples/example-03.kt renamed to kotlinx-coroutines-core/src/test/kotlin/examples/example-13.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package example13
22

33
import kotlinx.coroutines.experimental.*
44

kotlinx-coroutines-core/src/test/kotlin/examples/example-04.kt renamed to kotlinx-coroutines-core/src/test/kotlin/examples/example-14.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package example14
22

33
import kotlinx.coroutines.experimental.*
44

kotlinx-coroutines-core/src/test/kotlin/examples/example-05.kt renamed to kotlinx-coroutines-core/src/test/kotlin/examples/example-15.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package example15
22

33
import kotlinx.coroutines.experimental.*
44

kotlinx-coroutines-core/src/test/kotlin/examples/example-06.kt renamed to kotlinx-coroutines-core/src/test/kotlin/examples/example-16.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package example16
22

33
import kotlinx.coroutines.experimental.CommonPool
44
import kotlinx.coroutines.experimental.delay

kotlinx-coroutines-core/src/test/kotlin/examples/example-07.kt renamed to kotlinx-coroutines-core/src/test/kotlin/examples/example-21.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package example21
22

33
import kotlinx.coroutines.experimental.CommonPool
44
import kotlinx.coroutines.experimental.delay
@@ -13,8 +13,8 @@ fun main(args: Array<String>) = runBlocking<Unit> {
1313
}
1414
}
1515
delay(1300L) // delay a bit
16-
println("I'm tired of waiting!")
16+
println("main: I'm tired of waiting!")
1717
job.cancel() // cancels the job
1818
delay(1300L) // delay a bit to ensure it was cancelled indeed
19-
println("Now I can quit.")
19+
println("main: Now I can quit.")
2020
}

kotlinx-coroutines-core/src/test/kotlin/examples/example-08.kt renamed to kotlinx-coroutines-core/src/test/kotlin/examples/example-22.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package example22
22

33
import kotlinx.coroutines.experimental.CommonPool
44
import kotlinx.coroutines.experimental.delay
@@ -18,8 +18,8 @@ fun main(args: Array<String>) = runBlocking<Unit> {
1818
}
1919
}
2020
delay(1300L) // delay a bit
21-
println("I'm tired of waiting!")
21+
println("main: I'm tired of waiting!")
2222
job.cancel() // cancels the job
2323
delay(1300L) // delay a bit to see if it was cancelled....
24-
println("Now I can quit.")
24+
println("main: Now I can quit.")
2525
}

kotlinx-coroutines-core/src/test/kotlin/examples/example-09.kt renamed to kotlinx-coroutines-core/src/test/kotlin/examples/example-23.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package examples
1+
package example23
22

33
import kotlinx.coroutines.experimental.CommonPool
44
import kotlinx.coroutines.experimental.delay
@@ -18,8 +18,8 @@ fun main(args: Array<String>) = runBlocking<Unit> {
1818
}
1919
}
2020
delay(1300L) // delay a bit
21-
println("I'm tired of waiting!")
21+
println("main: I'm tired of waiting!")
2222
job.cancel() // cancels the job
2323
delay(1300L) // delay a bit to see if it was cancelled....
24-
println("Now I can quit.")
24+
println("main: Now I can quit.")
2525
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package example24
2+
3+
import kotlinx.coroutines.experimental.CommonPool
4+
import kotlinx.coroutines.experimental.delay
5+
import kotlinx.coroutines.experimental.launch
6+
import kotlinx.coroutines.experimental.runBlocking
7+
8+
fun main(args: Array<String>) = runBlocking<Unit> {
9+
val job = launch(CommonPool) {
10+
try {
11+
repeat(1000) { i ->
12+
println("I'm sleeping $i ...")
13+
delay(500L)
14+
}
15+
} finally {
16+
println("I'm running finally")
17+
}
18+
}
19+
delay(1300L) // delay a bit
20+
println("main: I'm tired of waiting!")
21+
job.cancel() // cancels the job
22+
delay(1300L) // delay a bit to ensure it was cancelled indeed
23+
println("main: Now I can quit.")
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package example25
2+
3+
import kotlinx.coroutines.experimental.*
4+
5+
fun main(args: Array<String>) = runBlocking<Unit> {
6+
val job = launch(CommonPool) {
7+
try {
8+
repeat(1000) { i ->
9+
println("I'm sleeping $i ...")
10+
delay(500L)
11+
}
12+
} finally {
13+
run(NonCancellable) {
14+
println("I'm running finally")
15+
delay(1000L)
16+
println("And I've just delayed for 1 sec because I'm non-cancellable")
17+
}
18+
}
19+
}
20+
delay(1300L) // delay a bit
21+
println("main: I'm tired of waiting!")
22+
job.cancel() // cancels the job
23+
delay(1300L) // delay a bit to ensure it was cancelled indeed
24+
println("main: Now I can quit.")
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package example26
2+
3+
import kotlinx.coroutines.experimental.delay
4+
import kotlinx.coroutines.experimental.runBlocking
5+
import kotlinx.coroutines.experimental.withTimeout
6+
7+
fun main(args: Array<String>) = runBlocking<Unit> {
8+
withTimeout(1300L) {
9+
repeat(1000) { i ->
10+
println("I'm sleeping $i ...")
11+
delay(500L)
12+
}
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package example31
2+
3+
import kotlinx.coroutines.experimental.delay
4+
import kotlinx.coroutines.experimental.runBlocking
5+
import kotlin.system.measureTimeMillis
6+
7+
suspend fun doSomethingUsefulOne(): Int {
8+
delay(1000L) // pretend we are doing something useful here
9+
return 13
10+
}
11+
12+
suspend fun doSomethingUsefulTwo(): Int {
13+
delay(1000L) // pretend we are doing something useful here, too
14+
return 29
15+
}
16+
17+
fun main(args: Array<String>) = runBlocking<Unit> {
18+
val time = measureTimeMillis {
19+
val one = doSomethingUsefulOne()
20+
val two = doSomethingUsefulTwo()
21+
println("The answer is ${one + two}")
22+
}
23+
println("Completed in $time ms")
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package example32
2+
3+
import kotlinx.coroutines.experimental.CommonPool
4+
import kotlinx.coroutines.experimental.defer
5+
import kotlinx.coroutines.experimental.delay
6+
import kotlinx.coroutines.experimental.runBlocking
7+
import kotlin.system.measureTimeMillis
8+
9+
suspend fun doSomethingUsefulOne(): Int {
10+
delay(1000L) // pretend we are doing something useful here
11+
return 13
12+
}
13+
14+
suspend fun doSomethingUsefulTwo(): Int {
15+
delay(1000L) // pretend we are doing something useful here, too
16+
return 29
17+
}
18+
19+
fun main(args: Array<String>) = runBlocking<Unit> {
20+
val time = measureTimeMillis {
21+
val one = defer(CommonPool) { doSomethingUsefulOne() }
22+
val two = defer(CommonPool) { doSomethingUsefulTwo() }
23+
println("The answer is ${one.await() + two.await()}")
24+
}
25+
println("Completed in $time ms")
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package example33
2+
3+
import kotlinx.coroutines.experimental.CommonPool
4+
import kotlinx.coroutines.experimental.delay
5+
import kotlinx.coroutines.experimental.lazyDefer
6+
import kotlinx.coroutines.experimental.runBlocking
7+
import kotlin.system.measureTimeMillis
8+
9+
suspend fun doSomethingUsefulOne(): Int {
10+
delay(1000L) // pretend we are doing something useful here
11+
return 13
12+
}
13+
14+
suspend fun doSomethingUsefulTwo(): Int {
15+
delay(1000L) // pretend we are doing something useful here, too
16+
return 29
17+
}
18+
19+
fun main(args: Array<String>) = runBlocking<Unit> {
20+
val time = measureTimeMillis {
21+
val one = lazyDefer(CommonPool) { doSomethingUsefulOne() }
22+
val two = lazyDefer(CommonPool) { doSomethingUsefulTwo() }
23+
println("The answer is ${one.await() + two.await()}")
24+
}
25+
println("Completed in $time ms")
26+
}

0 commit comments

Comments
 (0)