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.exampleSelect01 7 8 import kotlinx.coroutines.* 9 import kotlinx.coroutines.channels.* 10 import kotlinx.coroutines.selects.* 11 12 fun CoroutineScope.fizz() = produce<String> { 13 while (true) { // sends "Fizz" every 300 ms 14 delay(300) 15 send("Fizz") 16 } 17 } 18 <lambda>null19fun CoroutineScope.buzz() = produce<String> { 20 while (true) { // sends "Buzz!" every 500 ms 21 delay(500) 22 send("Buzz!") 23 } 24 } 25 selectFizzBuzznull26suspend fun selectFizzBuzz(fizz: ReceiveChannel<String>, buzz: ReceiveChannel<String>) { 27 select<Unit> { // <Unit> means that this select expression does not produce any result 28 fizz.onReceive { value -> // this is the first select clause 29 println("fizz -> '$value'") 30 } 31 buzz.onReceive { value -> // this is the second select clause 32 println("buzz -> '$value'") 33 } 34 } 35 } 36 <lambda>null37fun main() = runBlocking<Unit> { 38 val fizz = fizz() 39 val buzz = buzz() 40 repeat(7) { 41 selectFizzBuzz(fizz, buzz) 42 } 43 coroutineContext.cancelChildren() // cancel fizz & buzz coroutines 44 } 45