第一個(gè)協(xié)程
在使用協(xié)程之前差油,需要保證Kotlin-Gradle-Plugin
的版本高于1.3。目前最高的版本為1.3.11较幌。否則編譯會(huì)報(bào)錯(cuò)
首先來(lái)創(chuàng)建一個(gè)協(xié)程:
GlobalScope.launch { // 在后臺(tái)啟動(dòng)一個(gè)新的協(xié)程并繼續(xù)
delay(1000L) // 非阻塞的等待 1 秒鐘(默認(rèn)時(shí)間單位是毫秒)
println("World!...${Thread.currentThread().name}") // 在延遲后打印輸出
}
println("Hello,...${Thread.currentThread().name}") // 協(xié)程已在等待時(shí)主線程還在繼續(xù)
而打印出的結(jié)果為:
Hello,...main
World!...DefaultDispatcher-worker-1 @coroutine#1
可以看到通過(guò)GlobalScope.launch
完成了:
- 啟動(dòng)了新的線程集嵌,在該線程中創(chuàng)建了一個(gè)協(xié)程
@coroutine#1
- 在該協(xié)程中
delay(1000L)
等待了1秒后再輸出日志女仰。 - 并且這樣執(zhí)行的協(xié)程,并不會(huì)阻塞主線程的執(zhí)行
delay
函數(shù)只能在協(xié)程中使用轧抗,否則編譯不過(guò)恩敌,盡量避免使用GlobalScope.launch
創(chuàng)建協(xié)程,當(dāng)我們使用 GlobalScope.launch 時(shí)横媚,我們會(huì)創(chuàng)建一個(gè)頂層協(xié)程纠炮。雖然它很輕量,但它運(yùn)行時(shí)仍會(huì)消耗一些內(nèi)存資源灯蝴。如果我們忘記保持對(duì)新啟動(dòng)的協(xié)程的引用恢口,它還會(huì)繼續(xù)運(yùn)行。
阻塞的協(xié)程runBlocking
GlobalScope.launch
啟動(dòng)了一個(gè)線程創(chuàng)建新的協(xié)程绽乔,并沒(méi)有阻塞當(dāng)前線程弧蝇。而如果想要在當(dāng)前線程創(chuàng)建協(xié)程的話,則需要使用runBlocking
runBlocking {
launch {
Log.e(TAG,"${Thread.currentThread().name}...start delay 2000L")
delay(2000L)
Log.e(TAG,"${Thread.currentThread().name}...delay 2000L")
}
launch {
Log.e(TAG,"${Thread.currentThread().name}...start delay 4000L")
delay(4000L)
Log.e(TAG,"${Thread.currentThread().name}...delay 4000L")
}
Log.e(TAG,"${Thread.currentThread().name}...launch 2 coroutines")
}
從輸出結(jié)果折砸,可以看到:
- 直接在
runBlocking
中調(diào)用delay()
會(huì)阻塞當(dāng)前線程 - 在
runBlocking
中調(diào)用launch()
會(huì)開(kāi)啟新的協(xié)程看疗,并且不會(huì)阻塞當(dāng)前線程 - 在
runBlocking
中調(diào)用launch()
會(huì)在當(dāng)前線程中執(zhí)行協(xié)程
main @coroutine#1...launch 2 coroutines
main @coroutine#2...start delay 2000L
main @coroutine#3...start delay 4000L
main @coroutine#2...delay 2000L
main @coroutine#3...delay 4000L
等待協(xié)程執(zhí)行完畢
無(wú)論通過(guò)GlobalScope.launch
還是runBlocking
中的launch
,都會(huì)返回一個(gè)Job
對(duì)象睦授。通過(guò)Job.join
函數(shù)两芳,可以等待協(xié)程執(zhí)行完畢再繼續(xù)執(zhí)行。
val job = GlobalScope.launch { // 啟動(dòng)一個(gè)新協(xié)程并保持對(duì)這個(gè)作業(yè)的引用
delay(1000L)
println("Hello,...${Thread.currentThread().name}")
}
job.join() // 等待直到子協(xié)程執(zhí)行結(jié)束
println("World!...${Thread.currentThread().name}")
輸出結(jié)果:
Hello,...DefaultDispatcher-worker-1 @coroutine#2
World!...main @coroutine#1
CoroutineScope作用域
在runBlocking
中可以定義一個(gè)coroutineScope
去枷,而該函數(shù)的作用是為在這個(gè)函數(shù)中啟動(dòng)的協(xié)程添加作用域怖辆,只有當(dāng)作用域內(nèi)的協(xié)程都執(zhí)行完畢后,才會(huì)繼續(xù)執(zhí)行當(dāng)前線程删顶。
runBlocking {
Log.e(TAG, "${Thread.currentThread().name}...$this...start coroutineScope")
coroutineScope {
launch {
delay(4000L)
Log.e(TAG, "${Thread.currentThread().name}...$this...start delay 4000L")
}
launch {
delay(3000L)
Log.e(TAG, "${Thread.currentThread().name}...$this...start delay 3000L")
}
}
Log.e(TAG, "${Thread.currentThread().name}...$this...after launch ")
}
輸出的結(jié)果:
01-09 17:03:39.972 main...BlockingCoroutine{Active}@3fc800e3...start coroutineScope
01-09 17:03:39.972 main...StandaloneCoroutine{Active}@2affbae0...start delay 3000L
01-09 17:03:42.982 main...StandaloneCoroutine{Active}@23130199...start delay 4000L
01-09 17:03:43.982 main...BlockingCoroutine{Active}@3fc800e3...after launch
可以看到竖螃,在最后的日志打印之前,會(huì)等待coroutineScope
中的協(xié)程都執(zhí)行完畢后逗余,才會(huì)繼續(xù)執(zhí)行
掛起函數(shù)
當(dāng)協(xié)程中的代碼超級(jí)多的時(shí)候特咆,通常都會(huì)把這些代碼提取到一個(gè)函數(shù)中。但是這個(gè)函數(shù)必須使用suspend
標(biāo)識(shí)录粱,否則編譯錯(cuò)誤腻格,并且無(wú)法調(diào)用協(xié)程中到函數(shù)画拾,比如delay()
。
- 使用
suspend
標(biāo)示的函數(shù)只能用于協(xié)程中菜职,無(wú)法在其他函數(shù)中被調(diào)用
import kotlinx.coroutines.*
fun main() = runBlocking {
launch { doWorld() }
println("Hello,")
}
// 你的第一個(gè)掛起函數(shù)
suspend fun doWorld() {
delay(1000L)
println("World!")
}