launch

abstract fun launch(contextBuilder: CoroutineContext.() -> Unit?, start: (CoroutineStarts) -> CoroutineStart?, block: CoroutineScope.() -> Unit): Job

在此作用域内启动一个新的协程,而不会阻塞当前线程。

这是一个“即发即忘”(fire-and-forget)式的协程。它会立即返回一个 Job 对象, 而协程代码块将在后台并发执行。

Return

一个代表新协程的 Job 对象,可用于监控其状态或取消它。

Parameters

contextBuilder

一个可选的 lambda,用于为此特定协程自定义 CoroutineContext。 此上下文将与作用域的上下文合并。

start

协程的启动选项。例如,使用 it.getLazy() 来创建一个需要手动启动的协程。 默认为立即执行。

block

协程中要执行的代码块。该代码块将拥有一个 CoroutineScope 作为其接收者。