Skip to content

Commit 24cd654

Browse files
committed
Further improvements to testGuideCancelExample02/03 for stability
1 parent f1a24b1 commit 24cd654

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

coroutines-guide.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,13 @@ example shows:
353353

354354
```kotlin
355355
fun main(args: Array<String>) = runBlocking<Unit> {
356+
val startTime = System.currentTimeMillis()
356357
val job = launch(CommonPool) {
357-
var nextPrintTime = System.currentTimeMillis()
358+
var nextPrintTime = startTime
358359
var i = 0
359-
while (i < 10) { // computation loop
360-
val currentTime = System.currentTimeMillis()
361-
if (currentTime >= nextPrintTime) {
360+
while (i < 10) { // computation loop, just wastes CPU
361+
// print a message twice a second
362+
if (System.currentTimeMillis() >= nextPrintTime) {
362363
println("I'm sleeping ${i++} ...")
363364
nextPrintTime += 500L
364365
}
@@ -393,18 +394,19 @@ There are two approaches to making computation code cancellable. The first one i
393394
invoke a suspending function. There is a [yield] function that is a good choice for that purpose.
394395
The other one is to explicitly check the cancellation status. Let us try the later approach.
395396

396-
Replace `while (true)` in the previous example with `while (isActive)` and rerun it.
397+
Replace `while (i < 10)` in the previous example with `while (isActive)` and rerun it.
397398

398399
```kotlin
399400
fun main(args: Array<String>) = runBlocking<Unit> {
401+
val startTime = System.currentTimeMillis()
400402
val job = launch(CommonPool) {
401-
var nextPrintTime = 0L
403+
var nextPrintTime = startTime
402404
var i = 0
403405
while (isActive) { // cancellable computation loop
404-
val currentTime = System.currentTimeMillis()
405-
if (currentTime >= nextPrintTime) {
406+
// print a message twice a second
407+
if (System.currentTimeMillis() >= nextPrintTime) {
406408
println("I'm sleeping ${i++} ...")
407-
nextPrintTime = currentTime + 500L
409+
nextPrintTime += 500L
408410
}
409411
}
410412
}

kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-02.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ package guide.cancel.example02
2020
import kotlinx.coroutines.experimental.*
2121

2222
fun main(args: Array<String>) = runBlocking<Unit> {
23+
val startTime = System.currentTimeMillis()
2324
val job = launch(CommonPool) {
24-
var nextPrintTime = System.currentTimeMillis()
25+
var nextPrintTime = startTime
2526
var i = 0
26-
while (i < 10) { // computation loop
27-
val currentTime = System.currentTimeMillis()
28-
if (currentTime >= nextPrintTime) {
27+
while (i < 10) { // computation loop, just wastes CPU
28+
// print a message twice a second
29+
if (System.currentTimeMillis() >= nextPrintTime) {
2930
println("I'm sleeping ${i++} ...")
3031
nextPrintTime += 500L
3132
}

kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-03.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ package guide.cancel.example03
2020
import kotlinx.coroutines.experimental.*
2121

2222
fun main(args: Array<String>) = runBlocking<Unit> {
23+
val startTime = System.currentTimeMillis()
2324
val job = launch(CommonPool) {
24-
var nextPrintTime = 0L
25+
var nextPrintTime = startTime
2526
var i = 0
2627
while (isActive) { // cancellable computation loop
27-
val currentTime = System.currentTimeMillis()
28-
if (currentTime >= nextPrintTime) {
28+
// print a message twice a second
29+
if (System.currentTimeMillis() >= nextPrintTime) {
2930
println("I'm sleeping ${i++} ...")
30-
nextPrintTime = currentTime + 500L
31+
nextPrintTime += 500L
3132
}
3233
}
3334
}

0 commit comments

Comments
 (0)