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.exampleCancel05 7 8 import kotlinx.coroutines.* 9 10 fun main() = runBlocking { 11 val job = launch { 12 try { 13 repeat(1000) { i -> 14 println("job: I'm sleeping $i ...") 15 delay(500L) 16 } 17 } finally { 18 withContext(NonCancellable) { 19 println("job: I'm running finally") 20 delay(1000L) 21 println("job: And I've just delayed for 1 sec because I'm non-cancellable") 22 } 23 } 24 } 25 delay(1300L) // delay a bit 26 println("main: I'm tired of waiting!") 27 job.cancelAndJoin() // cancels the job and waits for its completion 28 println("main: Now I can quit.") 29 } 30