Skip to content

Instantly share code, notes, and snippets.

@elizarov
Created May 17, 2020 09:02
Show Gist options
  • Save elizarov/311b646c2fbf5fe19768fac3f21fe109 to your computer and use it in GitHub Desktop.
Save elizarov/311b646c2fbf5fe19768fac3f21fe109 to your computer and use it in GitHub Desktop.
Blocking input flow
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