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 select-expression.md by Knit tool. Do not edit. 6 package kotlinx.coroutines.guide.exampleSelect04 7 8 import kotlinx.coroutines.* 9 import kotlinx.coroutines.selects.* 10 import java.util.* 11 12 fun CoroutineScope.asyncString(time: Int) = async { 13 delay(time.toLong()) 14 "Waited for $time ms" 15 } 16 asyncStringsListnull17fun CoroutineScope.asyncStringsList(): List<Deferred<String>> { 18 val random = Random(3) 19 return List(12) { asyncString(random.nextInt(1000)) } 20 } 21 <lambda>null22fun main() = runBlocking<Unit> { 23 val list = asyncStringsList() 24 val result = select<String> { 25 list.withIndex().forEach { (index, deferred) -> 26 deferred.onAwait { answer -> 27 "Deferred $index produced answer '$answer'" 28 } 29 } 30 } 31 println(result) 32 val countActive = list.count { it.isActive } 33 println("$countActive coroutines are still active") 34 } 35