xref: /aosp_15_r20/external/kotlinx.coroutines/kotlinx-coroutines-core/jvm/test/guide/example-cancel-06.kt (revision 7a7160fed73afa6648ef8aa100d4a336fe921d9a)

<lambda>null1 // This file was automatically generated from cancellation-and-timeouts.md by Knit tool. Do not edit.
2 package kotlinx.coroutines.guide.exampleCancel06
3 
4 import kotlinx.coroutines.*
5 
6 fun main() = runBlocking {
7     val job = launch {
8         try {
9             repeat(1000) { i ->
10                 println("job: I'm sleeping $i ...")
11                 delay(500L)
12             }
13         } finally {
14             withContext(NonCancellable) {
15                 println("job: I'm running finally")
16                 delay(1000L)
17                 println("job: And I've just delayed for 1 sec because I'm non-cancellable")
18             }
19         }
20     }
21     delay(1300L) // delay a bit
22     println("main: I'm tired of waiting!")
23     job.cancelAndJoin() // cancels the job and waits for its completion
24     println("main: Now I can quit.")
25 }
26