Co-Routine, Co(์ /ํจ๊ป, ๋์์) ์ฝํ๋ฆฐ์ ์ฝ๋ฃจํด์ ๋ฌด์์ธ์ง, ๊ทธ๋ฆฌ๊ณ ์ ์ฌ์ฉ๋๋์ง ์์๋ณด๊ฒ ์ต๋๋ค. ์๋๋ก์ด๋ ๊ฐ์ด๋ ๋ฌธ์๋ฅผ ์ดํด๋ณด๋ฉด ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ ๊ฒฝ์ฐ, Coroutine์ ์ฌ์ฉํ๋๋ก ๊ถ์ฅํ๊ณ ์์ต๋๋ค.
๊ณต์๋ฌธ์์์๋ suspendable computations๋ผ๋ ํํ์ ํตํ์ฌ Coroutine์ ๋ํ ์ค๋ช ์ ์์ํฉ๋๋ค. ์ค๋จ์ํฌ ์ ์๋ ๊ณ์ฐ ์ ๋๋ก ํด์ํ๋ฉด ์ข์ ๊ฒ ๊ฐ์๋ฐ, ์ด ํํ ์์ฒด๊ฐ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ๊ฐ์ฅ ์ ์ ํ๊ฒ ํํํ ์ ์๋ค๋ ์๊ฐ์ด ๋ญ๋๋ค.

์๊ธฐ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ์ฝ๋ฃจํด์ ํ๋ก์ธ์ค์ ์์ ์ดํ ํ๋ก์ธ์ค๊ฐ ์ค๋จ๋์๋ค๊ฐ ๊ทธ ์ดํ ์ค๋จ๋ ๊ทธ ์์ ์ ๋ค์ ์ฌ๊ฐ๋ ์ ์๋๋ก ์ฝ๋๋ฅผ ๊ตฌ์ฑํ ์ ์์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ก์ธ์ค์ ์ค๊ฐ์ ๋๊ฐ๊ณ ๋ค์ ๋ค์ด์ฌ ๋, return๋ฌธ์ด ์์ด๋ ์ธ์ ๋ ์ง ๋๊ฐ๊ณ ๋ค์ ํด๋น ์ง์ ์ผ๋ก ๋ค์ด์ฌ ์ ์์ต๋๋ค.
์ด๋ฐ ์ฝ๋ฃจํด์ Thread์์ ์คํ๋๋๋ฐ, ํ๋์ Thread ๋ด์์ ์ฌ๋ฌ Coroutine๋ค์ด ๋์ํ ์ ์๊ณ Main Thread ์ด์ธ์ Sub Thread๊ฐ ์กด์ฌํ์ฌ ์์
์ด Non-blocking ํ ํํ๋ก ์ฌ๋ฌ Coroutine์ด ๋์ํ ์ ์๋ค.
์ฐธ๊ณ ์๋ฃ : ํ ๋ธ๋ก๊ทธ์ ์ด Thread ์์ ์ ๋ํ ๋ด์ฉ์ด ์ ์ ๋ฆฌ ๋์ด ์์ด ์ฒจ๋ถํ์์ต๋๋ค. / ์ดํด๊ฐ ์ฝ๊ฒ ๋๋ ๊ธ์ ๋๋ค.

์์ฃผ ๊ฐ๋จํ ์ฝ๋๋ค์ ํ์ฉํด์ Kotlin์์์ Coroutine์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค. ์ฃผ๋ ๋ด์ฉ์ Kotlin Document์์ ์ธ์ฉํ์์ต๋๋ค.
fun main() = runBlocking {
// this : CoroutineScope
launch {
delay(1000L)
}
}
runBlocking : ํ์ฌ Thread์์ Coroutine์ ์ฌ์ฉํ๊ธฐ ์ํ Coroutine Builder์
๋๋ค.
์ Coroutine์ ์คํํ๊ณ ์๋ฃ๋ ๋๊น์ง ํ์ฌ Main Thread๋ฅผ ์ฐจ๋จํฉ๋๋ค. ์ด๋ Coroutine์์ ์ฌ์ฉํ์ง ์๊ธฐ๋ฅผ ๊ถ์ฅํ๊ณ ์์ต๋๋ค.
expect fun <T> runBlocking(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> T
): T
launch : ์๋ ์ค์ธ ๋๋จธ์ง ์ฝ๋์ ๋์์ ์๋ก์ด Coroutine์ ์์ํฉ๋๋ค.
ํ์ฌ Thread๋ฅผ ์ฐจ๋จํ์ง ์๊ณ ์ Coroutine์ ์์ํ๋ฉฐ Coroutine์ ๋ํ ์ฐธ์กฐ๋ฅผ Job์ผ๋ก ๋ฐํํฉ๋๋ค.
fun CoroutineScope.launch(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job
delay : suspend function์ผ๋ก Thread๋ฅผ ์ฐจ๋จํ์ง ์๊ณ ์ฃผ์ด์ง ์๊ฐ ๋์ Coroutine์ ์ง์ฐํ๊ณ ์ง์ ํ ์๊ฐ ํ์ ๋ค์ ์์ํฉ๋๋ค.
suspend fun delay ( timeMillis : Long )
fun main() = runBlocking {
doWorld()
}
suspend fun doWorld() = coroutineScope {
launch {
delay(1000L)
}
}
coroutineScope : coroutineScope๋ฅผ ๋ง๋ค๊ณ ํด๋น ๋ฒ์์ ์ง์ ๋ suspend block์ ํธ์ถํ๋ค. ์ ๊ณต๋ ๋ฒ์๋ ์ธ๋ถ๋ฒ์์์ coroutineContext๋ฅผ ์์ํ์ง๋ง context์ Job์ ์ฌ์ ์ ํ๋ค. ์ด ํจ์๋ ํ์ coroutine์ด ์คํจํ๋ฉด ํด๋น ๋ฒ์ ๋ํ ์คํจํ์ฌ ๋๋จธ์ง ํ์ ํญ๋ชฉ์ด ๋ชจ๋ ์ทจ์๋๋ค. ์ด ํจ์๋ ์ง์ ๋ suspend block๊ณผ ํด๋น block์ ๋ชจ๋ ํ์ coroutine์ด ์๋ฃ๋๋ ์ฆ์ ๋ฐํ๋๋ค.
suspend fun <R> coroutineScope(
block: suspend CoroutineScope.() -> R
): R
supervisorScope : Supervisor Job์ผ๋ก coroutineScope๋ฅผ ๋ง๋ค๊ณ ์ด ๋ฒ์์์ ์ง์ ๋ suspend block์ ํธ์ถํ๋ค. ์ ๊ณต๋ ๋ฒ์๋ ์ธ๋ถ๋ฒ์์์ coroutineContext๋ฅผ ์์ํ์ง๋ง context์ Supervisor Job์ ์ฌ์ ์ ํ๋ค. ์ด ํจ์๋ ์ง์ ๋ block๊ณผ ๋ชจ๋ ํ์ coroutine์ด ์๋ฃ๋๋ ์ฆ์ ๋ฐํ๋๋ค.
coroutineScope์์ ์ฐจ์ด์
ํ์ ํญ๋ชฉ์ fail์ ๋ํ์ฌ ํด๋น ๋ฒ์์ fail๋ก ๊ฐ์ฃผํ์ง ์์ผ๋ฉฐ ๋ค๋ฅธ ํ์ ํญ๋ชฉ์ ๋ํ ์ํฅ์ ์ฃผ์ง ์๊ธฐ ๋๋ฌธ์ ํ์ ํญ๋ชฉ์ fail์ ๋ํ ์ฌ์ฉ์ ์ง์ ์ ์ฑ
์ ๊ตฌํํ ์ ์๋ค.
suspend fun <R> supervisorScope(
block: suspend CoroutineScope.() -> R
): R
val job = launch {
delay(1000L)
}
job.join()
job : ๊ฐ๋ ์ ์ผ๋ก ๋ฐฑ๊ทธ๋ผ์ด๋ ์์ ์ ์๋ฏธํ๋ฉฐ job์ completion๊ฐ ๋๋๋ life-cycle์์ ์ทจ์๊ฐ ๊ฐ๋ฅํ๋ค. job์ parent-child์ ๊ณ์ธต์ผ๋ก ์ ๋ ฌํ ์ ์์ผ๋ฉฐ parent ๊ณ์ธต์ด ์ทจ์๋๋ฉด child ๊ณ์ธต๋ ์ทจ์๋๋ค. failure๊ฐ ๋ฐ์ํ๋ CancellationException์ ๊ฒฝ์ฐ์๋ ์ฆ์ parent ๊ณ์ธต์ด ์ทจ์๋๋ฉฐ ๋ค๋ฅธ child ๊ณ์ธต์ ํญ๋ชฉ๋ค๋ ์ทจ์๋๋ค. ํด๋น ๋์์ ๋ํ์ฌ Supervisor Job์ ์ด์ฉํ์ฌ ์ฌ์ฉ์ ์ง์ ์ด ๊ฐ๋ฅํ๋ค.
interface Job : CoroutineContext.Element
Coroutine Job์ coroutine builder๋ฅผ ํตํด ๋ง๋ค์ด์ง๋ฉฐ ์ง์ ๋ ์ฝ๋ ๋ธ๋ก์ ์คํ์ํค๊ณ ํด๋น ๋ธ๋ก์ด ์๋ฃ๋๋ฉด Job๋ ์๋ฃ๋๋ค.
Completable Job์ Job() ํฉํ ๋ฆฌ ํจ์๋ก ์์ฑ๋๋ค. CompletableJob.complete๋ฅผ ํธ์ถํ๋ฉด ์๋ฃ๋๋ค.
Job์ ์คํ์ ๊ฒฐ๊ณผ ๊ฐ์ ์์ฑํ์ง ์์ผ๋ฉฐ, ์ค๋ก์ง side-effects๋ง์ ์ํด ์์ํ๋ค. Job์ ๋ํ ๊ฒฐ๊ณผ ์คํ์ ๋ค์ ํ์ด์ง๋ฅผ ์ฐธ์กฐ.
wait children
+-----+ start +--------+ complete +-------------+ finish +-----------+
| New | -----> | Active | ---------> | Completing | -------> | Completed |
+-----+ +--------+ +-------------+ +-----------+
| cancel / fail |
| +----------------+
| |
V V
+------------+ finish +-----------+
| Cancelling | --------------------------------> | Cancelled |
+------------+ +-----------+
'๐ป ๊ฐ๋ฐ์ ์ด์ผ๊ธฐ > ์๋๋ก์ด๋ ์ฝ์ง๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
kotlin-android-extensions ๋ฅผ ๊ฑท์ด๋ด์. ๊ฑท์ด๋ด์! (2) | 2024.03.22 |
---|---|
Android API Level 33 ์ ๋ฐ์ดํธ ์ ์ฉ (1) | 2023.08.22 |
Navigation, ์์ฃผ ๊ฐ๋จํ๊ฒ Fragment๊ฐ์ ์ ํ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ. (0) | 2022.10.20 |
[Android Build Issue] : [hilt] (2) | 2022.10.19 |
์ฝ๊ณ ๋น ๋ฅด๊ฒ ์ด๋ฏธ์ง ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ, Glide Library (0) | 2022.10.18 |