1 // This file was automatically generated from cancellation-and-timeouts.md by Knit tool. Do not edit. 2 package kotlinx.coroutines.guide.exampleCancel04 3 4 import kotlinx.coroutines.* 5 <lambda>null6fun main() = runBlocking { 7 val startTime = currentTimeMillis() 8 val job = launch(Dispatchers.Default) { 9 var nextPrintTime = startTime 10 var i = 0 11 while (isActive) { // cancellable computation loop 12 // print a message twice a second 13 if (currentTimeMillis() >= nextPrintTime) { 14 println("job: I'm sleeping ${i++} ...") 15 nextPrintTime += 500L 16 } 17 } 18 } 19 delay(1300L) // delay a bit 20 println("main: I'm tired of waiting!") 21 job.cancelAndJoin() // cancels the job and waits for its completion 22 println("main: Now I can quit.") 23 } 24