| // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. |
| package guide.cancel.example02 |
| |
| import kotlinx.coroutines.experimental.* |
| |
| fun main(args: Array<String>) = runBlocking<Unit> { |
| val job = launch(CommonPool) { |
| var nextPrintTime = 0L |
| var i = 0 |
| while (true) { // computation loop |
| val currentTime = System.currentTimeMillis() |
| if (currentTime >= nextPrintTime) { |
| println("I'm sleeping ${i++} ...") |
| nextPrintTime = currentTime + 500L |
| } |
| } |
| } |
| delay(1300L) // delay a bit |
| println("main: I'm tired of waiting!") |
| job.cancel() // cancels the job |
| delay(1300L) // delay a bit to see if it was cancelled.... |
| println("main: Now I can quit.") |
| } |