Kotlin中的线程和协程之间的区别 [英] Difference between thread and coroutine in Kotlin

查看:1563
本文介绍了Kotlin中的线程和协程之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kotlin中是否有任何特定的语言实现方式,与协同程序的其他语言实现方式不同?

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

  • 什么叫协程像轻量级线程?
  • 有什么区别?
  • kotlin协程是否实际上并行/并发运行?
  • 即使在多核系统中,任何给定时间都只有一个协程运行(是吗?)

在这里我要启动100000个协程,此代码后面会发生什么?

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

for(i in 0..100000){
   async(CommonPool){
    //run long running operations
  }
}

推荐答案

由于我仅在JVM上使用协程,因此我将讨论JVM后端,也有Kotlin Native和Kotlin JavaScript,但这些Kotlin后端不在我的讨论范围内

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.

因此,让我们开始比较Kotlin协程和其他语言的协程.基本上,您应该知道协程有两种类型:无堆栈和堆栈. Kotlin实现了无堆栈的协程-这意味着协程没有自己的堆栈,这限制了协程的功能.您可以阅读很好的解释

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.

示例:

  • 无堆栈:C#,Scala,Kotlin
  • 大量:Quasar,Javaflow

协程就像轻巧的线一样什么

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

这意味着Kotlin中的协程没有自己的堆栈,它不映射在本机线程上,不需要在处理器上进行上下文切换.

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.

线程-通常由OS管理. 协程-由用户管理.

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.

在这里我要启动100000个协程,此代码后面会发生什么?

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)
        }
    }
}

此代码立即执行.

因为我们需要等待async调用的结果.

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()
    }
}

运行此程序时,kotlin将创建2 * 100000个Continuation实例,这将占用几十Mb的RAM,在控制台中,您将看到1到100000之间的数字.

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()
}

我们现在实现了什么?现在,我们仅创建100001 Continuation实例,这要好得多.

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

每个创建的Continuation将在CommonPool(ForkJoinPool的静态实例)上调度和执行.

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

这篇关于Kotlin中的线程和协程之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆