Kotlin 协程

发布于:2025-05-20 ⋅ 阅读:(20) ⋅ 点赞:(0)
第一个协程程序

协程是可暂停计算的一个实例。它在概念上类似于线程,因为它需要运行一个代码块,该代码块与其他代码并发运行。然而,协程并不绑定到任何特定的线程。它可以在一个线程中暂停执行,并在另一个线程中恢复执行。

协程实例:

import kotlinx.coroutines.*

//sampleStart
fun main() = runBlocking { // this: CoroutineScope
    launch { // launch a new coroutine and continue
        delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
        println("World!") // print after delay
    }
    println("Hello") // main coroutine continues while a previous one is delayed
}
//sampleEnd

输出:

Hello
World!

launch是一个协程构建器。它会与其余代码同时启动一个新的协程,其余代码则继续独立运行。

Delay是一个特殊的暂停函数。它会将协程暂停一段时间。暂停协程不会阻塞底层线程,而是允许其他协程运行并使用底层线程执行其代码。

runBlocking也是一个协程构建器,它连接了常规的非协程世界fun main()和花括号内的协程代码runBlocking { ... }。如果缺少runBlocking { ... },则会在启动时收到错误提示:

Unresolved reference: launch

因为launch仅在CoroutineScope上声明。

顾名思义,运行runBlocking的线程(在本例中是主线程)在调用期间会被阻塞runBlocking { ... },直到所有协程都执行完毕。通常用在应用程序的最顶层,但在实际代码中很少见,因为线程是昂贵的资源,阻塞它们会导致效率低下。

结构化并发

协程遵循结构化并发原则,意味着新的协程只能在特定的CoroutineScope中启动,而CorountineScope限定了协程的生命周期。上面的示例表明runBlocking建立了相应的作用域。

在实际应用中,会启动大量协程。结构化并发可以确保它们不会丢失且不会泄露。外部作用域只有在其所有子协程完成后才能完成。结构化并发还能确保代码中的任何错误都能被正确报告,并且永远不会丢失。

提取函数重构

launch代码块提取重构到一个单独的函数中,会得到一个带有suspend修饰符的新函数,这是一个暂停函数,暂停函数可以像普通函数一样在协程内部使用,但它们的额外特性是,它们可以反过来使用其他暂停函数(如delay本例中所示)来暂停协程的执行。

import kotlinx.coroutines.*

//sampleStart
fun main() = runBlocking { // this: CoroutineScope
    launch { doWorld() }
    println("Hello")
}

// this is your first suspending function
suspend fun doWorld() {
    delay(1000L)
    println("World!")
}
//sampleEnd
范围构建器

除了不同构建器提供的协程作用域之外,还可以使用coroutineScope构建器声明自己的作用域。它会创建一个协程作用域,并且直到所有启动的子协程都完成后才会完成。

runBlockingcoroutineScope的构建器看起来相似,因为它们都等待其主体及其所有子协程完成。主要区别在于runBlocking方法会阻塞当前线程进行等待,而coroutineScope只是暂停,释放底层线程以用于其他用途。由于这一个区别,runBlocking是一个常规函数,而coroutineScope是一个暂停函数。

可以在任何暂停函数中使用coroutineScope。例如,你可以将Hello和 的并发打印World移到一个suspend fun doWorld()函数中:

import kotlinx.coroutines.*

//sampleStart
fun main() = runBlocking {
    doWorld()
}

suspend fun doWorld() = coroutineScope {  // this: CoroutineScope
    launch {
        delay(1000L)
        println("World!")
    }
    println("Hello")
}
//sampleEnd
范围构建器和并发

协程作用域构建器可以在任何挂起函数中使用,以执行多个并发操作。在一个doWorld挂起函数中启动两个并发协程:

import kotlinx.coroutines.*

//sampleStart
// Sequentially executes doWorld followed by "Done"
fun main() = runBlocking {
    doWorld()
    println("Done")
}

// Concurrently executes both sections
suspend fun doWorld() = coroutineScope { // this: CoroutineScope
    launch {
        delay(2000L)
        println("World 2")
    }
    launch {
        delay(1000L)
        println("World 1")
    }
    println("Hello")
}
//sampleEnd

块内的两段代码同时launch { ... }执行, 首先在启动后一秒打印,然后是启动后两秒打印。只有在两者完成后,协程作用域才会完成,因此只有在这之后才返回并允许打印字符串:

Hello
World 1
World 2
Done
明确的工作

启动协程构建器会返回一个Job对象,该对象是已启动协程的句柄,可用于显式等待其完成。例如,等待字协程完成,然后打印“Done”字符串:

import kotlinx.coroutines.*

fun main() = runBlocking {
//sampleStart
    val job = launch { // launch a new coroutine and keep a reference to its Job
        delay(1000L)
        println("World!")
    }
    println("Hello")
    job.join() // wait until child coroutine completes
    println("Done") 
//sampleEnd    
}

输出:

Hello
World!
Done
协程很轻量

协程比JVM线程占用更少的资源。使用线程时会耗尽JVM可用内存的代码,可以用协程来编写,而不会达到资源限制。例如,以下代码启动了50000个不同的协程,每个协程等待5秒,然后打印一个句点(“.”),同时消耗的内存非常少:

import kotlinx.coroutines.*

fun main() = runBlocking {
    repeat(50_000) { // 启动大量的协程
        launch {
            delay(5000L)
            print(".")
        }
    }
}

runBlocking如果使用线程(替换 launchthread,替换delayThread.sleep)编写相同的程序,它将消耗大量内存。根据操作系统、JDK 版本及其设置,要么会抛出内存不足错误,要么会缓慢启动线程,以确保并发运行的线程数量不会过多。


网站公告

今日签到

点亮在社区的每一天
去签到