question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Confuse at "Coroutine contexts can be combined using + operator"

See original GitHub issue

Well, 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:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
elizarovcommented, Oct 10, 2017

Coroutine context behaves like a map of context elements. You can run this code to get some intuition:

fun main(args: Array<String>) {
    println(mapOf(1 to "A") + mapOf(2 to "B")) // {1=A, 2=B}
    println(mapOf(1 to "A") + mapOf(1 to "B")) // {1=B}
    println(mapOf(1 to "B") + mapOf(1 to "A")) // {1=A}  
}

Does it help?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found