Confuse at "Coroutine contexts can be combined using + operator"
See original GitHub issueWell, a lot of questions about the +
operator for context.
I can use commonPool + job(or coroutineContext)
or job(or coroutineContext) + commonPool
do the same thing and canceling a launch
with calling cancel on job(or coroutineContext)
, however, when I use newSingleThreadContext
, there’s a difference between newSingleThreadContext + job(or coroutineContext)
and job(or coroutineContext) + newSingleThreadContext
. Only the first one can be canceled by job(or coroutineContext)
like commonPool
case.
Is it really not consistent of +
operator? need some tips on this topic.
Have some codes which are ready for the combination of all these dispatchers or context.
fun logln(msg: String) = println("[${Thread.currentThread().name}: ] $msg")
fun combineContext() = launch(newSingleThreadContext("worker-parent")) {
logln("Evaluation the combination of contexts")
logln("echo parent")
val job = Job() //Only for fun, it can abort all launches and make child not child of parent.
launch( //CommonPool
newSingleThreadContext("worker-child 0") + coroutineContext //+ job //
) {
delay(3, TimeUnit.SECONDS)
launch( //CommonPool
newSingleThreadContext("worker-child 1") + coroutineContext //+ job
) {
delay(3, TimeUnit.SECONDS)
launch( //CommonPool
newSingleThreadContext("worker-child 2") + coroutineContext //+ job
) {
delay(3, TimeUnit.SECONDS)
logln("echo child 3")
}
logln("echo child 2")
}
logln("echo child 1")
}
//job.cancel()
//coroutineContext.cancel()
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
"+" in Kotlin Coroutines? - operator overloading - Stack Overflow
The elements from this context with the same key as in the other one are dropped. It's basically a merge of two contexts....
Read more >Coroutine context and dispatchers - Kotlin
Combining context elements. Sometimes we need to define multiple elements for a coroutine context. We can use the + operator for that.
Read more >kotlinx-coroutines-core/common/src/flow/operators/Context.kt ...
It will use two coroutines for execution of the code. ... Multiple `flowOn` operators fuse to a single `flowOn` with a combined context....
Read more >Demystifying CoroutineContext - ProAndroidDev
With this, the + operator can be used to easily combine contexts with elements and elements with each other into a new context....
Read more >PEP 492 – Coroutines with async and await syntax
If the old protocol is used in 3.5.2, Python will raise a ... It is easy to confuse coroutines with regular generators, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Coroutine context behaves like a map of context elements. You can run this code to get some intuition:
Does it help?
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-single-thread-context.html
newSingleThreadContext
= dispatcher + job.