1 /* <lambda>null2 * 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.exampleCancel03 7 8 import kotlinx.coroutines.* 9 10 fun main() = runBlocking { 11 val job = launch(Dispatchers.Default) { 12 repeat(5) { i -> 13 try { 14 // print a message twice a second 15 println("job: I'm sleeping $i ...") 16 delay(500) 17 } catch (e: Exception) { 18 // log the exception 19 println(e) 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