<lambda>null1// This file was automatically generated from coroutine-context-and-dispatchers.md by Knit tool. Do not edit. 2 package kotlinx.coroutines.guide.exampleContext10 3 4 import kotlinx.coroutines.* 5 6 class Activity { 7 private val mainScope = CoroutineScope(Dispatchers.Default) // use Default for test purposes 8 9 fun destroy() { 10 mainScope.cancel() 11 } 12 13 fun doSomething() { 14 // launch ten coroutines for a demo, each working for a different time 15 repeat(10) { i -> 16 mainScope.launch { 17 delay((i + 1) * 200L) // variable delay 200ms, 400ms, ... etc 18 println("Coroutine $i is done") 19 } 20 } 21 } 22 } // class Activity ends 23 <lambda>null24fun main() = runBlocking<Unit> { 25 val activity = Activity() 26 activity.doSomething() // run test function 27 println("Launched coroutines") 28 delay(500L) // delay for half a second 29 println("Destroying activity!") 30 activity.destroy() // cancels all coroutines 31 delay(1000) // visually confirm that they don't work 32 } 33