Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

multithreading - CoroutineContext error appearing in channels

First of all, hello guys I am new here. Today I was just playing around with Kotlin Coroutines and Channels. From the official documentation, I saw one code snippet for channels like this:

val channel = Channel<Int>()
launch {
    // this might be heavy CPU-consuming computation or async logic, we'll just send five squares
    for (x in 1..5) channel.send(x * x)
}
// here we print five received integers:
repeat(5) { println(channel.receive()) }
println("Done!")

Then I decided to run this snippet in the Kotlin Playground with some modifications so that I could know how many coroutines are working.

My code:

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*

fun log(msg1:String ,msg2: Int) = println("[${Thread.currentThread().name}] $msg1 $msg2")

fun main() = runBlocking {
    val channel = Channel<Int>();
    
             launch {
               for (x in 1..5) {
                channel.send(x * x) 
                log("sending",x*x)
               }
             }
    
        repeat(5){log("receiving",channel.receive())}
   
    println("Done!")
}

As expected I received output:

[main @coroutine#2] sending 1
[main @coroutine#1] receiving 1
[main @coroutine#1] receiving 4
[main @coroutine#2] sending 4
[main @coroutine#2] sending 9
[main @coroutine#1] receiving 9
[main @coroutine#1] receiving 16
[main @coroutine#2] sending 16
[main @coroutine#2] sending 25
[main @coroutine#1] receiving 25
Done!

But then I tried to launch a seperate CoroutineScope as I watched the Kotlin Conference on YouTube in which 'Roman Elizarov' illustrated that we can transfer data between channels in different CoroutineScopes. So, I tried this:

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*

fun log(msg1:String ,msg2: Int) = println("[${Thread.currentThread().name}] $msg1 $msg2")

fun main() = runBlocking {
    val channel = Channel<Int>();
    
             launch {
               for (x in 1..5) {
                channel.send(x * x) 
                log("sending",x*x)
               }
             }
    
   
    CoroutineScope{
        launch{log("receiving",channel.receive())}
    }
   
    println("Done!")
}

After running this, I am getting an error as Type mismatch: inferred type is () -> Job but CoroutineContext was expected

Here's what I tried to rectify this error: I added Dispatchers.Default

CoroutineScope{
        launch(Dispatchers.Default){log("receiving",channel.receive())}
    }
   

Throwing same error. Apology for naive code.

Can somebody help?

question from:https://stackoverflow.com/questions/65950341/coroutinecontext-error-appearing-in-channels

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you are confusing CoroutineScope class with coroutineScope extension function. You are trying to call CoroutineScope constructor with the {launch{...}} block as an argument, but the constructor expects a CoroutineContext instead.

Change CoroutineScope to coroutineScope and add import for kotlinx.coroutines.coroutineScope.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...