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