xref: /aosp_15_r20/external/kotlinx.coroutines/kotlinx-coroutines-core/jvm/test/guide/example-flow-14.kt (revision 7a7160fed73afa6648ef8aa100d4a336fe921d9a)

<lambda>null1 // This file was automatically generated from flow.md by Knit tool. Do not edit.
2 package kotlinx.coroutines.guide.exampleFlow14
3 
4 import kotlinx.coroutines.*
5 import kotlinx.coroutines.flow.*
6 
7 fun simple(): Flow<Int> = flow {
8     // The WRONG way to change context for CPU-consuming code in flow builder
9     kotlinx.coroutines.withContext(Dispatchers.Default) {
10         for (i in 1..3) {
11             Thread.sleep(100) // pretend we are computing it in CPU-consuming way
12             emit(i) // emit next value
13         }
14     }
15 }
16 
<lambda>null17 fun main() = runBlocking<Unit> {
18     simple().collect { value -> println(value) }
19 }
20