jobs += launch(Unconfined) { println("'Unconfined': I'm working in thread ${Thread.currentThread().name}") } jobs += launch(coroutineContext) { println("'coroutineContext': I'm working in thread ${Thread.currentThread().name}") } jobs += launch(CommonPool) { println("'CommonPool': I'm working in thread ${Thread.currentThread().name}") } jobs += launch(newSingleThreadContext("MyOwnThread")) { println("'newSTC': I'm working in thread ${Thread.currentThread().name}") } jobs.forEach { it.join() } } 6ODPOGJOFE*NXPSLJOHJOUISFBENBJO $PNNPO1PPM*NXPSLJOHJOUISFBE'PSL+PJO1PPMDPNNPO1PPM XPSLFS OFX45$*NXPSLJOHJOUISFBE.Z0XO5ISFBE DPSPVUJOF$POUFYU*NXPSLJOHJOUISFBENBJO
{ try { repeat(1000) { i -> println("I'm sleeping $i ...") delay(500L) } } finally { println("I'm running finally") } } delay(1300L) // delay a bit println("main: I'm tired of waiting!") job.cancelAndJoin() // cancels the job and waits for its completion println("main: Now I can quit.") } I'm sleeping 0 ... I'm sleeping 1 ... I'm sleeping 2 ... main: I'm tired of waiting! I'm running finally main: Now I can quit.