Kotlin中的協(xié)程 - Scope

前言

Kotlin是一種在Java虛擬機(jī)上運(yùn)行的靜態(tài)類(lèi)型編程語(yǔ)言,被稱(chēng)之為Android世界的Swift,在GoogleI/O2017中,Google宣布Kotlin成為Android官方開(kāi)發(fā)語(yǔ)言

CoroutineScope

當(dāng)我們創(chuàng)建一個(gè)協(xié)程的時(shí)候,都會(huì)需要一個(gè)CoroutineScope,它是協(xié)程的作用域,我們一般使用它的launch函數(shù)以及async函數(shù)去進(jìn)行協(xié)程的創(chuàng)建

public fun CoroutineScope.launch(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> Unit
): Job {
    val newContext = newCoroutineContext(context)
    val coroutine = if (start.isLazy)
        LazyStandaloneCoroutine(newContext, block) else
        StandaloneCoroutine(newContext, active = true)
    coroutine.start(start, coroutine, block)
    return coroutine
}

Kotlin中的協(xié)程 - CoroutineContext 中我們了解到了,在launch函數(shù)中 具有傳遞上下文的功能,從而生成了Job鏈讓協(xié)程之間結(jié)構(gòu)化,并且我們的CoroutineContext也是定義在CoroutineScope當(dāng)中的

public interface CoroutineScope {
    /**
     * The context of this scope.
     * Context is encapsulated by the scope and used for implementation of coroutine builders that are extensions on the scope.
     * Accessing this property in general code is not recommended for any purposes except accessing [Job] instance for advanced usages.
     *
     * By convention, should contain an instance of a [job][Job] to enforce structured concurrency.
     */
    public val coroutineContext: CoroutineContext
}

常用函數(shù)

launch 啟動(dòng)一個(gè)協(xié)程代碼塊
async 返回值Deferred,表示一個(gè)延期返回的結(jié)果
coroutineContext 獲取當(dāng)前的coroutineContext
cancel 取消掉當(dāng)前Scope所對(duì)應(yīng)的協(xié)程

launch函數(shù)
launch通常用來(lái)創(chuàng)建一個(gè)協(xié)程,它是CoroutineScope的函數(shù)

public fun CoroutineScope.launch(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> Unit
): Job {
    val newContext = newCoroutineContext(context)
    val coroutine = if (start.isLazy)
        LazyStandaloneCoroutine(newContext, block) else
        StandaloneCoroutine(newContext, active = true)
    coroutine.start(start, coroutine, block)
    return coroutine
}

這個(gè)函數(shù)主要做了以下幾件事情:

  1. 將傳入的context與當(dāng)前Scopecontext進(jìn)行運(yùn)算,生成一個(gè)運(yùn)算之后的context,通過(guò)newCoroutineContext函數(shù),這個(gè)Context并不是最終協(xié)程的Context,而是它的父Context

2.如果是非lazy模式下創(chuàng)建一個(gè)Job,它是StandaloneCoroutine的對(duì)象,并且在父類(lèi)AbstractCoroutine中將創(chuàng)建的Job與上面生成的context在進(jìn)行plus運(yùn)算,最后才會(huì)生成此協(xié)程的Context對(duì)象

public final override val context: CoroutineContext = parentContext + this

3.然后在start中,將當(dāng)前協(xié)程的Job對(duì)象指定為ParentJob,形成了父子關(guān)聯(lián),這個(gè)操作是在JobSupportinitParentJobInternal函數(shù)中實(shí)現(xiàn)

    internal fun initParentJobInternal(parent: Job?) {
        val handle = parent.attachChild(this)
        parentHandle = handle
    }

整個(gè)過(guò)程中 Scope的工作,就是將Context進(jìn)行傳遞,使協(xié)程之間存在父子結(jié)構(gòu)化,使取消事件和異常行為進(jìn)行關(guān)聯(lián)

取消協(xié)程
Scope中也有函數(shù)可以對(duì)協(xié)程進(jìn)行取消

val scope = object : CoroutineScope {
    override val coroutineContext: CoroutineContext = Job()
}

//通過(guò)Scope創(chuàng)建一個(gè)協(xié)程
val job = scope.launch() {
    Log.e("Mike", "parent job start ${this.coroutineContext[Job]}")
  //通過(guò)同樣的Scope創(chuàng)建一個(gè)協(xié)程
    val chindJob = scope.launch() {
        Log.e("Mike", "child job start ${this.coroutineContext[Job]}")
        delay(5000)
        Log.e("Mike", "child job end ${this.coroutineContext[Job]}")
    }
    delay(4000)
    Log.e("Mike", "parent job end ${this.coroutineContext[Job]}")
}
Thread.sleep(1000)
job.cancel()
打印結(jié)果
parent job start
child job start
五秒后
child job end

上面的代碼中,雖然內(nèi)部的協(xié)程時(shí)在外部協(xié)程中進(jìn)行創(chuàng)建的,但是內(nèi)部協(xié)程并無(wú)法通過(guò)Job去進(jìn)行取消,原因就在于并沒(méi)有使用到傳遞的CoroutineScope,使兩個(gè)Job之間沒(méi)有結(jié)構(gòu)化關(guān)系,所以互不影響

//job.cancel()
scope.cancel()

換成scope中的cancel就可以正常的取消,因?yàn)閮蓚€(gè)協(xié)程的Job均是ScopeJobChild

val job = scope.launch() {
    val chindJob = launch() {
        delay(5000)
    }
    delay(4000)
}
Thread.sleep(1000)
job.cancel()

這時(shí)候Job之間使用CoroutineScope傳遞的Context,使Job之間有了關(guān)聯(lián),所以可以一起取消

MainScope與GlobalScope

GlobalScope中是一個(gè)EmptyCoroutineContext,其中并沒(méi)有Job對(duì)象,所以也無(wú)法通過(guò)GlobalScope去取消關(guān)聯(lián)的協(xié)程,所以它是進(jìn)程級(jí)別的Scope

public object GlobalScope : CoroutineScope {
    /**
     * Returns [EmptyCoroutineContext].
     */
    override val coroutineContext: CoroutineContext
        get() = EmptyCoroutineContext
}

因?yàn)?code>Scope也是通過(guò)Job去取消的

public fun CoroutineScope.cancel(cause: CancellationException? = null) {
    val job = coroutineContext[Job] ?: error("Scope cannot be cancelled because it does not have a job: $this")
    job.cancel(cause)
}

MianScope是由SupervisorJob和主線程調(diào)度組成的

public fun MainScope(): CoroutineScope = ContextScope(SupervisorJob() + Dispatchers.Main)

表明是無(wú)法被Cancel掉的

private class SupervisorJobImpl(parent: Job?) : JobImpl(parent) {
    override fun childCancelled(cause: Throwable): Boolean = false
}
val scope = object : CoroutineScope {
    override val coroutineContext: CoroutineContext = Job()
}
scope.launch {
    val response = async {
        delay(5000)
        "response data"
    }.await()
    Log.e("Mike", "async end")
    MainScope().launch {
        Log.e("Mike", "MainScope launch ")
    }
}
MainScope().cancel()
}
打印結(jié)果
5秒后
async end
MainScope launch

歡迎關(guān)注Mike的簡(jiǎn)書(shū)

Android 知識(shí)整理

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容