xref: /aosp_15_r20/external/kotlinx.coroutines/kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.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.exampleCancel03
3 
4 import kotlinx.coroutines.*
5 
6 fun main() = runBlocking {
7     val job = launch(Dispatchers.Default) {
8         repeat(5) { i ->
9             try {
10                 // print a message twice a second
11                 println("job: I'm sleeping $i ...")
12                 delay(500)
13             } catch (e: Exception) {
14                 // log the exception
15                 println(e)
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