Further improvements to testGuideCancelExample02/03 for stability
diff --git a/coroutines-guide.md b/coroutines-guide.md
index 64af574..4681f88 100644
--- a/coroutines-guide.md
+++ b/coroutines-guide.md
@@ -353,12 +353,13 @@
```kotlin
fun main(args: Array<String>) = runBlocking<Unit> {
+ val startTime = System.currentTimeMillis()
val job = launch(CommonPool) {
- var nextPrintTime = System.currentTimeMillis()
+ var nextPrintTime = startTime
var i = 0
- while (i < 10) { // computation loop
- val currentTime = System.currentTimeMillis()
- if (currentTime >= nextPrintTime) {
+ while (i < 10) { // computation loop, just wastes CPU
+ // print a message twice a second
+ if (System.currentTimeMillis() >= nextPrintTime) {
println("I'm sleeping ${i++} ...")
nextPrintTime += 500L
}
@@ -393,18 +394,19 @@
invoke a suspending function. There is a [yield] function that is a good choice for that purpose.
The other one is to explicitly check the cancellation status. Let us try the later approach.
-Replace `while (true)` in the previous example with `while (isActive)` and rerun it.
+Replace `while (i < 10)` in the previous example with `while (isActive)` and rerun it.
```kotlin
fun main(args: Array<String>) = runBlocking<Unit> {
+ val startTime = System.currentTimeMillis()
val job = launch(CommonPool) {
- var nextPrintTime = 0L
+ var nextPrintTime = startTime
var i = 0
while (isActive) { // cancellable computation loop
- val currentTime = System.currentTimeMillis()
- if (currentTime >= nextPrintTime) {
+ // print a message twice a second
+ if (System.currentTimeMillis() >= nextPrintTime) {
println("I'm sleeping ${i++} ...")
- nextPrintTime = currentTime + 500L
+ nextPrintTime += 500L
}
}
}
diff --git a/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-02.kt b/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-02.kt
index b79d525..1e99b34 100644
--- a/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-02.kt
+++ b/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-02.kt
@@ -20,12 +20,13 @@
import kotlinx.coroutines.experimental.*
fun main(args: Array<String>) = runBlocking<Unit> {
+ val startTime = System.currentTimeMillis()
val job = launch(CommonPool) {
- var nextPrintTime = System.currentTimeMillis()
+ var nextPrintTime = startTime
var i = 0
- while (i < 10) { // computation loop
- val currentTime = System.currentTimeMillis()
- if (currentTime >= nextPrintTime) {
+ while (i < 10) { // computation loop, just wastes CPU
+ // print a message twice a second
+ if (System.currentTimeMillis() >= nextPrintTime) {
println("I'm sleeping ${i++} ...")
nextPrintTime += 500L
}
diff --git a/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-03.kt b/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-03.kt
index 619b6b7..8746d38 100644
--- a/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-03.kt
+++ b/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-03.kt
@@ -20,14 +20,15 @@
import kotlinx.coroutines.experimental.*
fun main(args: Array<String>) = runBlocking<Unit> {
+ val startTime = System.currentTimeMillis()
val job = launch(CommonPool) {
- var nextPrintTime = 0L
+ var nextPrintTime = startTime
var i = 0
while (isActive) { // cancellable computation loop
- val currentTime = System.currentTimeMillis()
- if (currentTime >= nextPrintTime) {
+ // print a message twice a second
+ if (System.currentTimeMillis() >= nextPrintTime) {
println("I'm sleeping ${i++} ...")
- nextPrintTime = currentTime + 500L
+ nextPrintTime += 500L
}
}
}