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
186 views
in Technique[技术] by (71.8m points)

Difference between thread and coroutine in Kotlin

Is there any specific language implementation in Kotlin, which differs it from another languages implementation of coroutines?

  • What means that coroutine is like light-weight thread?
  • What is the difference?
  • Are kotlin coroutines actually running in parallely / concurrently?
  • Even in multi-core system, there is only one coroutine running at any given time (is it right?)

Here I'm starting 100000 coroutines, what happens behind this code?

for(i in 0..100000){
   async(CommonPool){
    //run long running operations
  }
}
question from:https://stackoverflow.com/questions/65845696/thread-coroutine-and-concurency

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

1 Answer

0 votes
by (71.8m points)

Since I used coroutines only on JVM, I will talk about JVM backend, there are also Kotlin Native and Kotlin JavaScript but these backends for Kotlin are out of my scope.

So let's start with comparing Kotlin coroutines to other languages coroutines. Basically, you should know that there are two types of Coroutines: stackless and stackful. Kotlin implements stackless coroutines - it means that coroutine doesn't have its own stack, and that limiting a little bit what coroutine can do. You can read a good explanation here.

Examples:

  • Stackless: C#, Scala, Kotlin
  • Stackful: Quasar, Javaflow

What it means that coroutine is like light-weight thread?

It means that coroutine in Kotlin doesn't have its own stack, it doesn't map on a native thread, it doesn't require context switching on a processor.

What is the difference?

Thread - preemptively multitasking. (usually). Coroutine - cooperatively multitasking.

Thread - managed by OS (usually). Coroutine - managed by a user.

Are kotlin's coroutines actually running in parallel / concurrently?

It depends, you can run each coroutine in its own thread, or you can run all coroutines in one thread or some fixed thread pool.

More about how coroutines execute here.

Even in a multi-core system, there is only one coroutine running at any given time (is it right?)

No, see the previous answer.

Here I'm starting 100000 coroutines, what happens behind this code?

Actually, it depends. But assume that you write the following code:

fun main(args: Array<String>) {
    for (i in 0..100000) {
        async(CommonPool) {
            delay(1000)
        }
    }
}

This code executes instantly.

Because we need to wait for results from async call.

So let's fix this:

fun main(args: Array<String>) = runBlocking {
    for (i in 0..100000) {
        val job = async(CommonPool) {
            delay(1)
            println(i)
        }

        job.join()
    }
}

When you run this program kotlin will create 2 * 100000 instances of Continuation, which will take a few dozen Mb of RAM, and in console, you will see numbers from 1 to 100000.

So lets rewrite this code in this way:

fun main(args: Array<String>) = runBlocking {

    val job = async(CommonPool) {
        for (i in 0..100000) {
            delay(1)
            println(i)
        }
    }

    job.join()
}

What we achieve now? Now we create only 100001 instances of Continuation, and this is much better.

Each created Continuation will be dispatched and executed on CommonPool (which is a static instance of ForkJoinPool).


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

...