1 /* 2 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 // This file was automatically generated from cancellation-and-timeouts.md by Knit tool. Do not edit. 6 package kotlinx.coroutines.guide.exampleCancel02 7 8 import kotlinx.coroutines.* 9 <lambda>null10fun main() = runBlocking { 11 val startTime = currentTimeMillis() 12 val job = launch(Dispatchers.Default) { 13 var nextPrintTime = startTime 14 var i = 0 15 while (i < 5) { // computation loop, just wastes CPU 16 // print a message twice a second 17 if (currentTimeMillis() >= nextPrintTime) { 18 println("job: I'm sleeping ${i++} ...") 19 nextPrintTime += 500L 20 } 21 } 22 } 23 delay(1300L) // delay a bit 24 println("main: I'm tired of waiting!") 25 job.cancelAndJoin() // cancels the job and waits for its completion 26 println("main: Now I can quit.") 27 } 28