Created
May 17, 2020 09:02
-
-
Save elizarov/311b646c2fbf5fe19768fac3f21fe109 to your computer and use it in GitHub Desktop.
Blocking input flow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.* | |
import kotlinx.coroutines.flow.* | |
fun main() = runBlocking<Unit> { | |
// working in the main thread | |
val stdinFlow = System.`in`.bufferedReader().lineSequence().asFlow() | |
// lets launch a coroutine that collects this flow and prints lines: | |
stdinFlow.onEach { println(it) }.launchIn(this) | |
// is the main thread active or blocked? | |
println("I'm not blocked yet") | |
// now yield main thread the the launched coroutines | |
yield() | |
// is it blocked now? | |
println("I'm still not blocked yet, hooray!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment