https://kotlinlang.org/docs/coroutines-basics.html
์ ๋งํฌ๋ฅผ ๊ณต๋ถํ๋ฉฐ ๋จ๊ธฐ๋ ๊ธฐ๋ก์ด๋ค.
์ฝ๋ฃจํด Coroutine
์ฝ๋ฃจํด์ ์ง์ฐ๊ฐ๋ฅํ ์ธ์คํด์ค์ด๋ค. ์ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ์ฐ๋ ๋์ ๋น์ทํ๊ฒ ๋์ง๋ง ํ ์ฐ๋ ๋ ์์์ ์ง์ฐ์์ ์ด ๊ฐ๋ฅํ๋ค.
์ง์ฐ๋๋ค๋ ์๋ฏธ๋ ํน์ ์์ ์ ๋ฉ์ถฐ๋๊ณ ์ฐ์ฐํ๋ค์, ์ด๊ฑธ ์ฌ์ฉํ ์ง ๋ง ์ง ๊ฒฐ์ ํ๋ค๊ณ ์ดํดํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค. ์ฝ๋ฃจํด์ ์ฐ๋ ๋์ ๋นํด ์ ์ ๋ฆฌ์์ค๋ฅผ ์ฌ์ฉํ๋ค๋ ์ฅ์ ์ด ์๋ค.
fun main() = runBlocking {
launch {
delay(2000L)
println("B!")
}
println("A")
}
์ํ ์ฝ๋๋ฅผ ์ด์ง๋ง ๋ฐ๊ฟ๋ดค๋ค. ์ฝ๋ ์์์ B->A๊ฐ ๋ผ์ผํ์ง๋ง B๊ฐ 2์ด ๋๋ ์ด ๊ฑธ๋ฆฌ๋๋์์ A๊ฐ ๋จผ์ ์ถ๋ ฅ๋ผ์ A->B๊ฐ ๋๋ค.
runBlocking{}
runBlocking์ ์ฝ๋ฃจํด ๋น๋์ด์ ์ค๊ฐ๋ค๋ฆฌ ์ญํ ์ ํ๋ค. main()ํจ์๋ฅผ runBlocking์ผ๋ก ๋ฌถ์ผ๋ฉด์ ์ฝ๋ฃจํด ์ฝ๋๋ฅผ ์ ๋ ฅํ ์ ์๊ฒ ํด์คฌ๋ค. ์ฝ๋ฃจํด ์ค์ฝํ๊ฐ ์๋๊ณณ์์ ์ฝ๋ฃจํด ์ฝ๋๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ํด์ฃผ๋ ์ญํ ์ ํ๋ค. ์ ํํ๋ runBlocking ๋ด๋ถ์ ์ฝ๋ฃจํด ์ฝ๋๊ฐ ์ข ๋ฃ๋ ๋๊น์ง ์ง๊ธ ์ฌ์ฉ์ค์ธ ์ฐ๋ ๋๋ฅผ ์ ์ (block)ํ๋ ํจ์๋ผ๊ณ ํ ์ ์๋ค. ๋ฉ์ธ ์ฐ๋ ๋๋ฅผ ์ ์ ํ๋ฉด ๋ฆฌ์์ค ๋ถ์กฑ์ผ๋ก ์ธํ ์๋ฌ๊ฐ ๋ฐ์ํ ์ ์์ผ๋ฏ๋ก ์ฌ์ฉ์ ์ฃผ์๊ฐ ํ์ํ๋ค.
launch{}
launch๋ ์ฝ๋ฃจํด ๋น๋(์ฝ๋ฃจํด์ ์์ฑํ๋ ์ญํ )์ด๋ค. launch๋ก ๊ฐ์ผ ์ค์ฝํ๋ ๋ ๋ฆฝ์ ์ผ๋ก ์คํ๋๋ค. ํ์ฌ ์ฐ๋ ๋๋ฅผ ์ ์ ํ์ง ์์ง๋ง ์ฝ๋ฃจํด ์ค์ฝํ ๋ด์์๋ง ์กด์ฌํ ์ ์๋ค. Job์ ๋ฐํํ๋๋ฐ ์ด job์ด cancelled๋ผ๋ฉด ํ์ฌ ์ฝ๋ฃจํด์ด ์ทจ์๋๋ค.
์ฝ๋ฃจํด์ ๊ตฌ์กฐ์ ๋์์ฑ ์์น(structured concurrency)์ ๋ฐ๋ฅธ๋ค๊ณ ํ๋ค. ์ ์ฝ๋ฃจํด์ ์ค์ง ์ฝ๋ฃจํด ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ตฌ๋ถ ํ ์ ์๋ ์ฝ๋ฃจํด ์ค์ฝํ ์์์๋ง ์์๋ ์ ์๋ค. ๋ถ๋ชจ ์ฝ๋ฃจํด์ ์์ ์ฝ๋ฃจํด์ด ์๋ฃ๋๊ธฐ ์ ๊น์ง ์๋ฃ๋ ์ ์๋ค. ์ด ๋ถ๋ถ์ด ๋๋ฝ, ๋์๊ฐ ์์์ ๋ณด์ฅํ๋ ์ด์ ๋ผ๊ณ ํ๋ค.
launch ์์ ์ฝ๋๋ ๋ ๋ฆฝ์ ์ด๋ค. ๊ทธ๋ฆฌ๊ณ ์ฝ๋ฃจํด์ ์ง์ฐ๊ฐ๋ฅํ ์์ ์ด๋ค. ์ด ๋ ๊ฐ๋ฅผ ํฉ์น๋ฉด ๋์ค๋ ๊ฐ๋ ์ด suspend fun์ด๋ค.
suspend fun doWorld() {
delay(2000)
println("A")
}
suspend๋ก ์ง์ ํ ํจ์๋ ์ฝ๋ฃจํด ๋ด๋ถ์์ ์ผ๋ฐ ํจ์์ฒ๋ผ ์๋ํ๋ค. ๋ ์ด ํจ์๋ฅผ ํตํด ๋ ๋ค๋ฅธ ์์ ์ ์ง์ฐ์ํฌ ์ ์๋ค.
coroutineScope - ์ค์ฝํ ๋น๋ Scope builder
์ฝ๋ฃจํด ์ค์ฝํ๋ฅผ ์ ๊ณตํ๋ ์ฌ๋ฌ ์ค์ฝํ ๋น๋๋ค์ด ์๋๋ฐ ๊ทธ์ค ํ๋๋ก coroutineScope๊ฐ ์๋ค.
runBlocking๊ณผ coroutineScope์ ์ฐจ์ด์ ์ ์ง์์ฑ์ด๋ค. runBlocking์ ์ฐ๋ ๋๋ฅผ ์์ ์ ์ ํด๋ฒ๋ฆฌ์ง๋ง coroutineScope๋ ํต์งธ๋ก ๊ฐ์ ธ๊ฐ๋๊ฒ ์๋๋ผ ํน์ ์์ ์ ๋ฉ์ถฐ๋ ๋ ๋ฆฝ๋ ์ฐ๋ ๋์ฒ๋ผ ์๋ํ๋ค. ๊ทธ๋์ runBlocking์ ์ผ๋ฐํจ์, coroutineScope๋ suspending ํจ์์ด๋ค.
fun main() = runBlocking {
doWorld()
}
suspend fun doWorld() = coroutineScope { // this: CoroutineScope
launch {
delay(1000L)
println("B")
}
println("A")
}
launch๋ ์ฝ๋ฃจํด ์ค์ฝํ์์์ ์คํํ ์ ์๊ธฐ ๋๋ฌธ์ suspend fun์์ launch๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ผ๋ฉด coroutineScope๋ก ๊ฐ์ธ์ฃผ๋ฉด ๋๋ค. ์ ์ฝ๋์ ๊ฒฝ์ฐ coroutineScope๊ฐ ์ข ๋ฃ๋๊ณ ๋์ runBlocking์ด ์ข ๋ฃ๋๋ค.
// Concurrently executes both sections
suspend fun doWorld() = coroutineScope { // this: CoroutineScope
launch {
delay(2000L)
println("A")
}
launch {
delay(1000L)
println("B")
}
println("C")
}
launch๋ฅผ ํ ์ฝ๋ฃจํด์ค์ฝํ ์์์ 2๊ฐ ์คํํ๋ฉด ๋์์ ๋์๊ฐ๋ค. ์ ์ฝ๋๋ C->B->A๋ฅผ ์ถ๋ ฅํ๋ค.
Job
์์์ launch๋ job์ ๋ฐํํ๋ค๊ณ ์ ์์๋ค.
val job = launch { // launch a new coroutine and keep a reference to its Job
delay(1000L)
println("B")
}
println("A")
job.join() // wait until child coroutine completes
println("C")
์ด ์ฝ๋๋ ์ฝ๋ฃจํด ์ค์ฝํ ์์ ์๋ค๊ณ ๊ฐ์ ํ๋ค. job.join()์ ์ด์ฉํ๋ฉด ์ง๊ธ B๊ฐ ๋ด๊ธด job์ด ๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค. A->B->C๊ฐ ์ถ๋ ฅ๋๋ค. ์ด job ์ธ์คํด์ค๋ ์ทจ์ ์ํฌ์๋์๋ค.
์ด ๊ทธ๋ฆผ์ ๋ณด๋ฉด ์คํ์ค์ ์ทจ์ ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
a: https://ko.wikipedia.org/wiki/%EB%A9%80%ED%8B%B0%EC%8A%A4%EB%A0%88%EB%94%A9
b: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/
'Android ๐ฅ๏ธ > Coroutine๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Coroutine exceptions handling (0) | 2024.03.29 |
---|---|
Asynchronous Flow - Intermediate flow operators~ (1) | 2024.03.29 |
Coroutine context and dispatchers (0) | 2024.03.29 |
Coroutine Composing suspending functions (0) | 2024.03.29 |
Coroutine Cancellation and timeouts (0) | 2024.03.29 |