Skip to content

Instantly share code, notes, and snippets.

@uzzu
Created August 6, 2019 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uzzu/c58f1f86ed778c69d566e5439af7b704 to your computer and use it in GitHub Desktop.
Save uzzu/c58f1f86ed778c69d566e5439af7b704 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
class Sample(override val coroutineContext: CoroutineContext) : CoroutineScope {
fun foo() =
launch {
println("1. launched foo @ $coroutineContext")
val flow = flowFoo()
flow.collect {
println("3-5. collected flowFoo @ $coroutineContext, value: $it")
}
}
fun fooIo() =
launch(Dispatchers.IO) {
println("1. launched fooIo @ $coroutineContext")
val flow = flowFoo()
flow.collect {
println("3-5. collected flowFoo @ $coroutineContext, value: $it")
}
}
fun fooWithContext() =
launch {
println("1. launched fooWithContext @ $coroutineContext")
val flow = withContext(Dispatchers.IO) { flowFoo() }
flow.collect {
println("3-5. collected flowFoo @ $coroutineContext, value: $it")
}
}
@ExperimentalCoroutinesApi
fun fooWithFlowOn() =
launch {
println("1. launched fooWithFlowOn @ $coroutineContext")
val flow = flowFoo().flowOn(Dispatchers.IO)
flow.collect {
println("3-5. collected flowFoo @ $coroutineContext, value: $it")
}
}
@ExperimentalCoroutinesApi
fun fooWithFlowOnOuter(): Job {
val flow = flowFoo().flowOn(Dispatchers.IO)
return launch {
println("1. launched fooWithFlowOnOuter @ $coroutineContext")
flow.collect {
println("3-5. collected flowFoo @ $coroutineContext, value: $it")
}
}
}
fun flowFoo() = flow {
println("2. called flowFoo @ $coroutineContext")
emit(100)
emit(200)
emit(300)
println("6. emitted flow foo @ $coroutineContext")
println("7. completed flow foo @ $coroutineContext")
}
}
fun main() = runBlocking {
println("start")
val sample = Sample(coroutineContext)
sample.foo().join()
println("---------")
sample.fooIo().join()
println("---------")
sample.fooWithContext().join()
println("---------")
sample.fooWithFlowOn().join()
println("---------")
sample.fooWithFlowOnOuter().join()
println("---------")
}
main()
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
scratch.kts:89 1. launched foo @ [StandaloneCoroutine{Active}@32a1bec0, BlockingEventLoop@22927a81]
scratch.kts:89 2. called flowFoo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@32a1bec0, BlockingEventLoop@22927a81], value: 100
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@32a1bec0, BlockingEventLoop@22927a81], value: 200
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@32a1bec0, BlockingEventLoop@22927a81], value: 300
scratch.kts:89 6. emitted flow foo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 7. completed flow foo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 ---------
scratch.kts:89 1. launched fooIo @ [StandaloneCoroutine{Active}@73cd03d9, LimitingDispatcher@36cf83c4[dispatcher = DefaultDispatcher]]
scratch.kts:89 2. called flowFoo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@73cd03d9, LimitingDispatcher@36cf83c4[dispatcher = DefaultDispatcher]], value: 100
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@73cd03d9, LimitingDispatcher@36cf83c4[dispatcher = DefaultDispatcher]], value: 200
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@73cd03d9, LimitingDispatcher@36cf83c4[dispatcher = DefaultDispatcher]], value: 300
scratch.kts:89 6. emitted flow foo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 7. completed flow foo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 ---------
scratch.kts:89 1. launched fooWithContext @ [StandaloneCoroutine{Active}@4f8e5cde, BlockingEventLoop@22927a81]
scratch.kts:89 2. called flowFoo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@4f8e5cde, BlockingEventLoop@22927a81], value: 100
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@4f8e5cde, BlockingEventLoop@22927a81], value: 200
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@4f8e5cde, BlockingEventLoop@22927a81], value: 300
scratch.kts:89 6. emitted flow foo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 7. completed flow foo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 ---------
scratch.kts:89 1. launched fooWithFlowOn @ [StandaloneCoroutine{Active}@5fcfe4b2, BlockingEventLoop@22927a81]
scratch.kts:89 2. called flowFoo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 6. emitted flow foo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 7. completed flow foo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@5fcfe4b2, BlockingEventLoop@22927a81], value: 100
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@5fcfe4b2, BlockingEventLoop@22927a81], value: 200
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@5fcfe4b2, BlockingEventLoop@22927a81], value: 300
scratch.kts:89 ---------
scratch.kts:89 1. launched fooWithFlowOnOuter @ [StandaloneCoroutine{Active}@782830e, BlockingEventLoop@22927a81]
scratch.kts:89 2. called flowFoo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 6. emitted flow foo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 7. completed flow foo @ [BlockingCoroutine{Active}@3581c5f3, BlockingEventLoop@22927a81]
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@782830e, BlockingEventLoop@22927a81], value: 100
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@782830e, BlockingEventLoop@22927a81], value: 200
scratch.kts:89 3-5. collected flowFoo @ [StandaloneCoroutine{Active}@782830e, BlockingEventLoop@22927a81], value: 300
scratch.kts:89 ---------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment