欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文徹底搞懂Kotlin中的協(xié)程

 更新時(shí)間:2021年03月18日 10:32:54   作者:Jason灬不棄  
這篇文章主要給大家介紹了Kotlin中協(xié)程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

產(chǎn)生背景

為了解決異步線程產(chǎn)生的回調(diào)地獄

//傳統(tǒng)回調(diào)方式
api.login(phone,psd).enquene(new Callback<User>(){
 public void onSuccess(User user){
 api.submitAddress(address).enquene(new Callback<Result>(){
 public void onSuccess(Result result){
 ...
 }
 });
 }
});
//使用協(xié)程后
val user=api.login(phone,psd)
api.submitAddress(address)
...

協(xié)程是什么

本質(zhì)上,協(xié)程是輕量級(jí)的線程。

協(xié)程關(guān)鍵名詞

val job = GlobalScope.launch {
 delay(1000)
 println("World World!")
}

CoroutineScope(作用范圍)

控制協(xié)程代碼塊執(zhí)行的線程,生命周期等,包括GlobeScope、lifecycleScope、viewModelScope以及其他自定義的CoroutineScope

GlobeScope:全局范圍,不會(huì)自動(dòng)結(jié)束執(zhí)行

lifecycleScope:生命周期范圍,用于activity等有生命周期的組件,在DESTROYED的時(shí)候會(huì)自動(dòng)結(jié)束,需額外引入

viewModelScope:viewModel范圍,用于ViewModel中,在ViewModel被回收時(shí)會(huì)自動(dòng)結(jié)束,需額外引入

Job(作業(yè))

協(xié)程的計(jì)量單位,相當(dāng)于一次工作任務(wù),launch方法默認(rèn)返回一個(gè)新的Job

suspend(掛起)

作用于方法上,代表該方法是耗時(shí)任務(wù),例如上面的delay方法

public suspend fun delay(timeMillis: Long) {
 ...
}

協(xié)程的引入

主框架($coroutines_version替換為最新版本,如1.3.9,下同)

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"

lifecycleScope(可選,版本2.2.0)

implementation 'androidx.activity:activity-ktx:$lifecycle_scope_version'

viewModelScope(可選,版本2.3.0-beta01)

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$coroutines_viewmodel_version"

簡單使用

先舉個(gè)簡單例子

lifecycleScope.launch { 
 delay(2000)
 tvTest.text="Test"
}

上面這個(gè)例子實(shí)現(xiàn)的功能是等待2秒,然后修改id為tvTest的TextView控件的text值為Test

自定義延遲返回方法

在kotlin里面,對(duì)于需要延遲才能返回結(jié)果的方法,需要用suspend標(biāo)明

lifecycleScope.launch {
 val text=getText()
 tvTest.text = text
}
suspend fun getText():String{
 delay(2000)
 return "getText"
}

如果在其他線程,需要使用Continuation進(jìn)行線程切換,可使用suspendCancellableCoroutine 或 suspendCoroutine包裹(前者可取消,相當(dāng)于后者的擴(kuò)展),成功調(diào)用it.resume(),失敗調(diào)用it.resumeWithException(Exception()),拋出異常

suspend fun getTextInOtherThread() = suspendCancellableCoroutine<String> {
 thread {
 Thread.sleep(2000)
 it.resume("getText")
 }
}

異常捕獲

協(xié)程里面的失敗都可以通過異常捕獲,來統(tǒng)一處理特殊情況

lifecycleScope.launch {
 try {
 val text=getText()
 tvTest.text = text
 } catch (e:Exception){
 e.printStackTrace()
 }
}

取消功能

下面執(zhí)行了兩個(gè)job,第一個(gè)是原始的,第二個(gè)是在1秒后取消第一個(gè)job,這會(huì)導(dǎo)致tvText的文本并不會(huì)改變

val job = lifecycleScope.launch {
 try {
 val text=getText()
 tvTest.text = text
 } catch (e:Exception){
 e.printStackTrace()
 }
}
lifecycleScope.launch {
 delay(1000)
 job.cancel()
}

設(shè)置超時(shí)

這個(gè)相當(dāng)于系統(tǒng)封裝了自動(dòng)取消功能,對(duì)應(yīng)函數(shù)withTimeout

lifecycleScope.launch {
 try {
 withTimeout(1000) {
  val text = getText()
  tvTest.text = text
 }
 } catch (e:Exception){
 e.printStackTrace()
 }
}

帶返回值的Job

與launch類似的還有一個(gè)async方法,它會(huì)返回一個(gè)Deferred對(duì)象,屬于Job的擴(kuò)展類,Deferred可以獲取返回的結(jié)果,具體使用如下

lifecycleScope.launch {
 val one= async {
 delay(1000)
 return@async 1
 }
 val two= async {
 delay(2000)
 return@async 2
 }
 Log.i("scope test",(one.await()+two.await()).toString())
}

高級(jí)進(jìn)階

自定義CoroutineScope

先看CoroutineScope源碼

public interface CoroutineScope {
 public val coroutineContext: CoroutineContext
}

CoroutineScope中主要包含一個(gè)coroutineContext對(duì)象,我們要自定義只需實(shí)現(xiàn)coroutineContext的get方法

class TestScope() : CoroutineScope {
 override val coroutineContext: CoroutineContext
  get() = TODO("Not yet implemented")
}

要?jiǎng)?chuàng)建coroutineContext,得要先知道CoroutineContext是什么,我們?cè)倏碈oroutineContext源碼

/**
 * Persistent context for the coroutine. It is an indexed set of [Element] instances.
 * An indexed set is a mix between a set and a map.
 * Every element in this set has a unique [Key].
 */
public interface CoroutineContext {
 public operator fun <E : Element> get(key: Key<E>): E?
 public fun <R> fold(initial: R, operation: (R, Element) -> R): R
 public operator fun plus(context: CoroutineContext): CoroutineContext = 
  ...
 public fun minusKey(key: Key<*>): CoroutineContext
 
 public interface Key<E : Element>
 public interface Element : CoroutineContext {
  ...
 }
}

通過注釋說明,我們知道它本質(zhì)就是一個(gè)包含Element的集合,只是不像set和map集合一樣,它自己實(shí)現(xiàn)了獲?。╣et),折疊(fold,添加和替換的組合),相減(minusKey,移除),對(duì)象組合(plus,如val coroutineContext=coroutineContext1+coroutineContext2)
它的主要內(nèi)容是Element,而Element的實(shí)現(xiàn)有

  • Job 任務(wù)
  • ContinuationInterceptor 攔截器
  • AbstractCoroutineContextElement
  • CoroutineExceptionHandler
  • ThreadContextElement
  • DownstreamExceptionElement
  • ....

可以看到很多地方都有實(shí)現(xiàn)Element,它主要目的是限制范圍以及異常的處理。這里我們先了解兩個(gè)重要的Element,一個(gè)是Job,一個(gè)是CoroutineDispatcher
Job

  • Job:子Job取消,會(huì)導(dǎo)致父job和其他子job被取消;父job取消,所有子job被取消
  • SupervisorJob:父job取消,所有子job被取消

CoroutineDispatcher

  • Dispatchers.Main:主線程執(zhí)行
  • Dispatchers.IO:IO線程執(zhí)行

我們模擬一個(gè)類似lifecycleScope的自定義TestScope

class TestScope() : CoroutineScope {
 override val coroutineContext: CoroutineContext
  get() = SupervisorJob() +Dispatchers.Main
}

這里我們定義了一個(gè)總流程線SupervisorJob()以及具體執(zhí)行環(huán)境Dispatchers.Main(Android主線程),假如我們想替換掉activity的lifecycleScope,就需要在activity中創(chuàng)建實(shí)例

val testScope=TestScope()

然后在activity銷毀的時(shí)候取消掉所有job

override fun onDestroy() {
 testScope.cancel()
 super.onDestroy()
}

其他使用方式同lifecycleScope,如

testScope.launch{
 val text = getText()
 tvTest.text = text
}

深入理解Job

CoroutineScope中包含一個(gè)主Job,之后調(diào)用的launch或其他方法創(chuàng)建的job都屬于CoroutineScope的子Job,每個(gè)job都有屬于自己的狀態(tài),其中包括isActive、isCompleted、isCancelled,以及一些基礎(chǔ)操作start()、cancel()、join(),具體的轉(zhuǎn)換流程如下

我們先從創(chuàng)建job開始,當(dāng)調(diào)用launch的時(shí)候默認(rèn)有三個(gè)參數(shù)CoroutineContext、CoroutineStart以及代碼塊參數(shù)。

  • context:CoroutineContext的對(duì)象,默認(rèn)為CoroutineStart.DEFAULT,會(huì)與CoroutineScope的context進(jìn)行折疊
  • start:CoroutineStart的對(duì)象,默認(rèn)為CoroutineStart.DEFAULT,代表立即執(zhí)行,同時(shí)還有CoroutineStart.LAZY,代表非立即執(zhí)行,必須調(diào)用job的start()才會(huì)開始執(zhí)行
val job2= lifecycleScope.launch(start = CoroutineStart.LAZY) {
 delay(2000)
 Log.i("scope test","lazy")
}
job2.start()

當(dāng)使用這種模式創(chuàng)建時(shí)默認(rèn)就是new狀態(tài),此時(shí)isActive,isCompleted,isCancelled都為false,當(dāng)調(diào)用start后,轉(zhuǎn)換為active狀態(tài),其中只有isActive為true,如果它的任務(wù)完成了則會(huì)進(jìn)入Completing狀態(tài),此時(shí)為等待子job完成,這種狀態(tài)下還是只有isActive為true,如果所有子job也完成了則會(huì)進(jìn)入Completed狀態(tài),只有isCompleted為true。如果在active或Completing狀態(tài)下出現(xiàn)取消或異常,則會(huì)進(jìn)入Cancelling狀態(tài),如果需要取消父job和其他子job則會(huì)等待它們?nèi)∠瓿?,此時(shí)只有isCancelled為true,取消完成后最終進(jìn)入Cancelled狀態(tài),isCancelled和isCompleted都為true

State isActive isCompleted isCancelled
New FALSE FALSE FALSE
Active TRUE FALSE FALSE
Completing TRUE FALSE FALSE
Cancelling FALSE FALSE TRUE
Cancelled FALSE TRUE TRUE
Completed FALSE TRUE FALSE

不同job交互需使用join()與cancelAndJoin()

  • join():將當(dāng)前job添加到其他協(xié)程任務(wù)里面
  • cancelAndJoin():取消操作,只是添加進(jìn)去后再取消
val job1= GlobleScope.launch(start = CoroutineStart.LAZY) {
 delay(2000)
 Log.i("scope test","job1")
}
lifecycleScope.launch {
 job1.join()
 delay(2000)
 Log.i("scope test","job2")
}

深入理解suspend

suspend作為kotlin新增的方法修飾詞,最終實(shí)現(xiàn)還是java,我們先看它們的差異性

suspend fun test1(){}
fun test2(){}

對(duì)應(yīng)java代碼

public final Object test1(@NotNull Continuation $completion) {
 return Unit.INSTANCE;
}
public final void test2() {
}

對(duì)應(yīng)字節(jié)碼

public final test1(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
 ...
 L0
 LINENUMBER 6 L0
 GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit;
 ARETURN
 L1
 LOCALVARIABLE this Lcom/lieni/android_c/ui/test/TestActivity; L0 L1 0
 LOCALVARIABLE $completion Lkotlin/coroutines/Continuation; L0 L1 1
 MAXSTACK = 1
 MAXLOCALS = 2

public final test2()V
 L0
 LINENUMBER 9 L0
 RETURN
 L1
 LOCALVARIABLE this Lcom/lieni/android_c/ui/test/TestActivity; L0 L1 0
 MAXSTACK = 0
 MAXLOCALS = 1

可以看到,加了suspend的方法其實(shí)和普通方法一樣,只是傳入時(shí)多了個(gè)Continuation對(duì)象,并返回了Unit.INSTANCE對(duì)象

public interface Continuation<in T> {
  public val context: CoroutineContext
  public fun resumeWith(result: Result<T>)
}

而Continuation的具體實(shí)現(xiàn)在BaseContinuationImpl中

internal abstract class BaseContinuationImpl(...) : Continuation<Any?>, CoroutineStackFrame, Serializable {
  public final override fun resumeWith(result: Result<Any?>) {
    ...
    while (true) {
      ...
      with(current) {
       	val outcome = invokeSuspend(param)
        ...
        releaseIntercepted() 
        if (completion is BaseContinuationImpl) {
          ...
        } else {
          ...
          return
        }
      }
    }
  }
  ...
}

當(dāng)我們調(diào)用resumeWith時(shí),它會(huì)一直執(zhí)行一個(gè)循環(huán),調(diào)用invokeSuspend(param)和releaseIntercepted() ,直到最頂層completion執(zhí)行完成后返回,并且釋放協(xié)程的interceptor

最終的釋放在ContinuationImpl中實(shí)現(xiàn)

internal abstract class ContinuationImpl(...) : BaseContinuationImpl(completion) {
  ...
  protected override fun releaseIntercepted() {
    val intercepted = intercepted
    if (intercepted != null && intercepted !== this) {
      context[ContinuationInterceptor]!!.releaseInterceptedContinuation(intercepted)
    }
    this.intercepted = CompletedContinuation 
  }
}

通過這里知釋放最終通過CoroutineContext中為ContinuationInterceptor的Element來實(shí)現(xiàn)
而暫停也是同理,繼續(xù)看suspendCoroutine

public suspend inline fun <T> suspendCoroutine(crossinline block: (Continuation<T>) -> Unit): T =
  suspendCoroutineUninterceptedOrReturn { c: Continuation<T> ->
    val safe = SafeContinuation(c.intercepted())
    ...
  }

默認(rèn)會(huì)調(diào)用Continuation的intercepted()方法

internal abstract class ContinuationImpl(...) : BaseContinuationImpl(completion) {
  ...
  public fun intercepted(): Continuation<Any?> =intercepted
      ?: (context[ContinuationInterceptor]?.interceptContinuation(this) ?: this)
        .also { intercepted = it }
}

可以看到暫停最終也是通過CoroutineContext中為ContinuationInterceptor的Element來實(shí)現(xiàn)

流程總結(jié)(線程切換)

  • 創(chuàng)建新的Continuation
  • 調(diào)用CoroutineScope中的context的ContinuationInterceptor的interceptContinuation方法暫停父任務(wù)
  • 執(zhí)行子任務(wù)(如果指定了線程,則在新線程執(zhí)行,并傳入Continuation對(duì)象)
  • 執(zhí)行完畢后用戶調(diào)用Continuation的resume或者resumeWith返回結(jié)果
  • 調(diào)用CoroutineScope中的context的ContinuationInterceptor的releaseInterceptedContinuation方法恢復(fù)父任務(wù)

阻塞與非阻塞

CoroutineScope默認(rèn)是不會(huì)阻塞當(dāng)前線程的,如果需要阻塞可以使用runBlocking,如果在主線程執(zhí)行下面代碼,會(huì)出現(xiàn)2s白屏

runBlocking { 
  delay(2000)
  Log.i("scope test","runBlocking is completed")
}

阻塞原理:執(zhí)行runBlocking默認(rèn)會(huì)創(chuàng)建BlockingCoroutine,而BlockingCoroutine中會(huì)一直執(zhí)行一個(gè)循環(huán),直到當(dāng)前Job為isCompleted狀態(tài)才會(huì)跳出循環(huán)

public fun <T> runBlocking(...): T {
  ...
  val coroutine = BlockingCoroutine<T>(newContext, currentThread, eventLoop)
  coroutine.start(CoroutineStart.DEFAULT, coroutine, block)
  return coroutine.joinBlocking()
}
private class BlockingCoroutine<T>(...) : AbstractCoroutine<T>(parentContext, true) {
  ...
  fun joinBlocking(): T {
   ...
   while (true) {
    ...
    if (isCompleted) break
    ...
   }  
   ...
  }
}

總結(jié)

到此這篇關(guān)于一文徹底搞懂Kotlin中協(xié)程的文章就介紹到這了,更多相關(guān)Kotlin協(xié)程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論