一文徹底搞懂Kotlin中的協(xié)程
產(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é)程是輕量級的線程。
協(xié)程關(guān)鍵名詞
val job = GlobalScope.launch {
delay(1000)
println("World World!")
}
CoroutineScope(作用范圍)
控制協(xié)程代碼塊執(zhí)行的線程,生命周期等,包括GlobeScope、lifecycleScope、viewModelScope以及其他自定義的CoroutineScope
GlobeScope:全局范圍,不會自動結(jié)束執(zhí)行
lifecycleScope:生命周期范圍,用于activity等有生命周期的組件,在DESTROYED的時候會自動結(jié)束,需額外引入
viewModelScope:viewModel范圍,用于ViewModel中,在ViewModel被回收時會自動結(jié)束,需額外引入
Job(作業(yè))
協(xié)程的計量單位,相當(dāng)于一次工作任務(wù),launch方法默認(rèn)返回一個新的Job
suspend(掛起)
作用于方法上,代表該方法是耗時任務(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"
簡單使用
先舉個簡單例子
lifecycleScope.launch {
delay(2000)
tvTest.text="Test"
}
上面這個例子實(shí)現(xiàn)的功能是等待2秒,然后修改id為tvTest的TextView控件的text值為Test
自定義延遲返回方法
在kotlin里面,對于需要延遲才能返回結(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í)行了兩個job,第一個是原始的,第二個是在1秒后取消第一個job,這會導(dǎo)致tvText的文本并不會改變
val job = lifecycleScope.launch {
try {
val text=getText()
tvTest.text = text
} catch (e:Exception){
e.printStackTrace()
}
}
lifecycleScope.launch {
delay(1000)
job.cancel()
}
設(shè)置超時
這個相當(dāng)于系統(tǒng)封裝了自動取消功能,對應(yīng)函數(shù)withTimeout
lifecycleScope.launch {
try {
withTimeout(1000) {
val text = getText()
tvTest.text = text
}
} catch (e:Exception){
e.printStackTrace()
}
}
帶返回值的Job
與launch類似的還有一個async方法,它會返回一個Deferred對象,屬于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ìn)階
自定義CoroutineScope
先看CoroutineScope源碼
public interface CoroutineScope {
public val coroutineContext: CoroutineContext
}
CoroutineScope中主要包含一個coroutineContext對象,我們要自定義只需實(shí)現(xiàn)coroutineContext的get方法
class TestScope() : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = TODO("Not yet implemented")
}
要創(chuàng)建coroutineContext,得要先知道CoroutineContext是什么,我們再看CoroutineContext源碼
/**
* 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ì)就是一個包含Element的集合,只是不像set和map集合一樣,它自己實(shí)現(xiàn)了獲?。╣et),折疊(fold,添加和替換的組合),相減(minusKey,移除),對象組合(plus,如val coroutineContext=coroutineContext1+coroutineContext2)
它的主要內(nèi)容是Element,而Element的實(shí)現(xiàn)有
- Job 任務(wù)
- ContinuationInterceptor 攔截器
- AbstractCoroutineContextElement
- CoroutineExceptionHandler
- ThreadContextElement
- DownstreamExceptionElement
- ....
可以看到很多地方都有實(shí)現(xiàn)Element,它主要目的是限制范圍以及異常的處理。這里我們先了解兩個重要的Element,一個是Job,一個是CoroutineDispatcher
Job
- Job:子Job取消,會導(dǎo)致父job和其他子job被取消;父job取消,所有子job被取消
- SupervisorJob:父job取消,所有子job被取消
CoroutineDispatcher
- Dispatchers.Main:主線程執(zhí)行
- Dispatchers.IO:IO線程執(zhí)行
我們模擬一個類似lifecycleScope的自定義TestScope
class TestScope() : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = SupervisorJob() +Dispatchers.Main
}
這里我們定義了一個總流程線SupervisorJob()以及具體執(zhí)行環(huán)境Dispatchers.Main(Android主線程),假如我們想替換掉activity的lifecycleScope,就需要在activity中創(chuàng)建實(shí)例
val testScope=TestScope()
然后在activity銷毀的時候取消掉所有job
override fun onDestroy() {
testScope.cancel()
super.onDestroy()
}
其他使用方式同lifecycleScope,如
testScope.launch{
val text = getText()
tvTest.text = text
}
深入理解Job
CoroutineScope中包含一個主Job,之后調(diào)用的launch或其他方法創(chuàng)建的job都屬于CoroutineScope的子Job,每個job都有屬于自己的狀態(tài),其中包括isActive、isCompleted、isCancelled,以及一些基礎(chǔ)操作start()、cancel()、join(),具體的轉(zhuǎn)換流程如下

我們先從創(chuàng)建job開始,當(dāng)調(diào)用launch的時候默認(rèn)有三個參數(shù)CoroutineContext、CoroutineStart以及代碼塊參數(shù)。
- context:CoroutineContext的對象,默認(rèn)為CoroutineStart.DEFAULT,會與CoroutineScope的context進(jìn)行折疊
- start:CoroutineStart的對象,默認(rèn)為CoroutineStart.DEFAULT,代表立即執(zhí)行,同時還有CoroutineStart.LAZY,代表非立即執(zhí)行,必須調(diào)用job的start()才會開始執(zhí)行
val job2= lifecycleScope.launch(start = CoroutineStart.LAZY) {
delay(2000)
Log.i("scope test","lazy")
}
job2.start()
當(dāng)使用這種模式創(chuàng)建時默認(rèn)就是new狀態(tài),此時isActive,isCompleted,isCancelled都為false,當(dāng)調(diào)用start后,轉(zhuǎn)換為active狀態(tài),其中只有isActive為true,如果它的任務(wù)完成了則會進(jìn)入Completing狀態(tài),此時為等待子job完成,這種狀態(tài)下還是只有isActive為true,如果所有子job也完成了則會進(jìn)入Completed狀態(tài),只有isCompleted為true。如果在active或Completing狀態(tài)下出現(xiàn)取消或異常,則會進(jìn)入Cancelling狀態(tài),如果需要取消父job和其他子job則會等待它們?nèi)∠瓿?,此時只有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(){}
對應(yīng)java代碼
public final Object test1(@NotNull Continuation $completion) {
return Unit.INSTANCE;
}
public final void test2() {
}
對應(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í)和普通方法一樣,只是傳入時多了個Continuation對象,并返回了Unit.INSTANCE對象
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時,它會一直執(zhí)行一個循環(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)會調(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對象)
- 執(zhí)行完畢后用戶調(diào)用Continuation的resume或者resumeWith返回結(jié)果
- 調(diào)用CoroutineScope中的context的ContinuationInterceptor的releaseInterceptedContinuation方法恢復(fù)父任務(wù)
阻塞與非阻塞
CoroutineScope默認(rèn)是不會阻塞當(dāng)前線程的,如果需要阻塞可以使用runBlocking,如果在主線程執(zhí)行下面代碼,會出現(xiàn)2s白屏
runBlocking {
delay(2000)
Log.i("scope test","runBlocking is completed")
}
阻塞原理:執(zhí)行runBlocking默認(rèn)會創(chuàng)建BlockingCoroutine,而BlockingCoroutine中會一直執(zhí)行一個循環(huán),直到當(dāng)前Job為isCompleted狀態(tài)才會跳出循環(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)下拉刷新的視圖和圖標(biāo)的旋轉(zhuǎn)
本篇文章主要介紹了Android實(shí)現(xiàn)下拉刷新的視圖和圖標(biāo)的旋轉(zhuǎn)的實(shí)例,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
Android Studio3.6設(shè)置Gradle Offline Mode的方法
這篇文章主要介紹了Android Studio3.6設(shè)置Gradle Offline Mode的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android開發(fā)中通過手機(jī)號+短信驗(yàn)證碼登錄的實(shí)例代碼
最近在開發(fā)一個android的項(xiàng)目,需要通過獲取手機(jī)驗(yàn)證碼來完成登錄功能,接下來通過實(shí)例代碼給大家分享手機(jī)號+短信驗(yàn)證碼登錄的實(shí)現(xiàn)方法,需要的的朋友參考下吧2017-05-05
Android 優(yōu)雅的實(shí)現(xiàn)通用格式化編輯
這篇文章主要介紹了Android 優(yōu)雅的實(shí)現(xiàn)通用格式化編輯,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Flutter 完美的驗(yàn)證碼輸入框?qū)崿F(xiàn)
這篇文章主要介紹了Flutter 完美的驗(yàn)證碼輸入框?qū)崿F(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Android自定義listview布局實(shí)現(xiàn)上拉加載下拉刷新功能
這篇文章主要介紹了Android自定義listview布局實(shí)現(xiàn)上拉加載下拉刷新功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12
Android獲取手機(jī)型號/系統(tǒng)版本號/App版本號等信息實(shí)例講解
本示例獲得手機(jī)型號,系統(tǒng)版本,App版本號等信息,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android使用NumberPicker實(shí)現(xiàn)滑輪日期選擇器
這篇文章主要為大家介紹了如何使用Android中的NumberPicker控件,以一種簡單而直觀的方式實(shí)現(xiàn)滑輪式的日期選擇器,需要的小伙伴可以參考一下2023-06-06

