kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html
Job - kotlinx-coroutines-core
Job A background job. Conceptually, a job is a cancellable thing with a life-cycle that culminates in its completion. Jobs can be arranged into parent-child hierarchies where cancellation of a parent leads to immediate cancellation of all its children recu
kotlin.github.io
interface Job : Element
๋ฐฑ๊ทธ๋ผ์ด๋ ์์ . ๊ฐ๋ ์ ์ผ๋ก๋ ์์ ์ ์๋ฃ๋ก ๋๋๋ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , ์ทจ์ํ ์ ์๋ค.
์์-ํ์ ๊ณ์ธต/๋ถ๋ชจ-์์ ๊ณ์ธต์ผ๋ก ์ด๋ฃจ์ด์ ธ ์์ด์ ๋ถ๋ชจ๊ฐ ์ทจ์๋๋ฉด ๋ชจ๋ ์๋ ๋ค๋ ๋ฐ๋ก ์ฌ๊ท์ ์ผ๋ก ์ทจ์๋๋ค.
CancellationExeption ์ธ์ exception์ผ๋ก ์์์ด ์ทจ์๋๋ฉด ์ฆ์ ๋ถ๋ชจ์ ๋ถ๋ชจ์ ๋ชจ๋ ๋ค๋ฅธ ์๋ ๋ค๋ ์ทจ์๋๋ค. SupervisorJob์ผ๋ก ์ด ๋์์ ์ฌ์ฉ์๊ฐ ์ง์ ํ ์ ์๋ค.
๊ธฐ๋ณธ์ ์ธ Job์ ์ธ์คํด์ค์ ์์ฑ์ ๋ค์๊ณผ ๊ฐ๋ค.
- launch ์ฝ๋ฃจํด ๋น๋๋ก Coroutine job์ด ์์ฑ๋๋ค. ํน์ ๋ธ๋ก ์์์ ์คํํ๊ณ ๋ธ๋ก์ ์๋ฃ ๋ ์๋ฃ๋๋ค.
- Job() ํฉํ ๋ฆฌ ํจ์๋ก CompletableJob์ด ์์ฑ๋๋ค. CompletableJob.complete ํจ์๊ฐ ํธ์ถ๋๋ฉด ์๋ฃ๋๋ค.
๊ฐ๋ ์ ์ผ๋ก Job์ ์คํ์ ๊ฒฐ๊ณผ ๊ฐ์ ๋ง๋ค์ด๋ด์ง ์๋๋ค.
์ํ
Job์ ์ํ(state)๋ฅผ ๊ฐ์ง๋ค. ์ํ๋ isActive, isComplete, isCancelled ๋ก ํ์ธํ ์ ์๋ค.
wait children
+-----+ start +--------+ complete +-------------+ finish +-----------+
| New | -----> | Active | ---------> | Completing | -------> | Completed |
+-----+ +--------+ +-------------+ +-----------+
| cancel / fail |
| +----------------+
| |
V V
+------------+ finish +-----------+
| Cancelling | --------------------------------> | Cancelled |
+------------+ +-----------+
๋๊ฒ ์ฝ๋ฃจํด์ active ์ํ๋ก ์์ฑ๋๋ค. ์ฝ๋ฃจํด ๋น๋๊ฐ CoroutineStart.LAZY ๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฃ์ผ๋ฉด new ์ํ์ ์ฝ๋ฃจํด์ ์์ฑํด start() ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ์ด๋ฌํ job์ start๋๋ join์ผ๋ก active ์ํ๊ฐ ๋๋ค.
Job์ active ์ํ๋ 1) ์ฝ๋ฃจํด์ด ์์ ํ๋ ๋์, 2) CompleteableJob์ด ์๋ฃ(complete)๋๊ธฐ ์ ๊น์ง, ๋๋ 3) ์ฝ๋ฃจํด์ด fail๋๊ฑฐ๋ cancell๋๊ธฐ ์ ๊น์ง์ด๋ค.
activeํ Job์ exception์ด ์๊ฒจ ์คํจํ๋ฉด cancelling ์ํ๊ฐ ๋๋ค. Job์ ์ธ์ ๋ cancel ํจ์๋ก ์ทจ์ ๊ฐ๋ฅํ ์ฆ์ cancelling ์ํ๋ก ์ ํ๋๋ค. Job์ด ๋ชจ๋ ์์ ์ ๋ง์น๊ณ ๋ชจ๋ ์๋ ๋ค์ด ์๋ฃ๋๋ฉด cancelled ์ํ๊ฐ ๋๋ค.
activeํ ์ฝ๋ฃจํด์ body์ ์๋ฃ๋ CompletableJob.complete๊ฐ ํธ์ถ๋๋ฉด Job์ completing ์ํ๊ฐ ๋๋ค. completed ์ํ๊ฐ ๋๊ธฐ ์ ์ ๋ชจ๋ ์๋ ๋ค์ด ์๋ฃ๋๊ธฐ๋ฅผ ๊ธฐ๋ค๋ฆฐ๋ค. completing ์ํ๋ Job์ ๋ด๋ถ์ ์ธ ์ํ๋ผ์ ์๋ ๋ค์ ์๋ฃ๋ฅผ ๋ด๋ถ์ ์ผ๋ก ๊ธฐ๋ค๋ฆฌ๋ ๋์์ ๋ฐ๊นฅ์์๋ active ์ํ๋ก ํ์ธ๋๋ค.
์ทจ์ ์์ธ
Job์ body๊ฐ exception์ ๋ฐ์์ํค๋ฉด ์ฝ๋ฃจํด Job์ ์์ธ์ ์ผ๋ก ์๋ฃ๋๋ค๊ณ ํ๋ค. CompletableJob์ CompletableJob.completeExeptionally๋ฅผ ํธ์ถํด์ ์์ธ์ ์ผ๋ก ์๋ฃ๋๋ค. ์์ธ์ ์ผ๋ก ์๋ฃ๋ Job์ ์ทจ์๋๊ณ ํด๋น exception์ Job์ ์ทจ์ ์์ธ์ด ๋๋ค.
Job์ ์ทจ์๋ฅผ ๋ฐ์์ํจ exception์ ์ข ๋ฅ์ ๋ฐ๋ผ์ ์คํจ์ ์ ์์ ์ธ ์ทจ์๊ฐ ๊ตฌ๋ถ๋๋ค.
- CancellationException์ด ์ ์์ ์ผ๋ก ์ทจ์์ํค๊ณ ,
- ๊ทธ ์ธ ๋ค๋ฅธ exception๋ค์ ์คํจ์ํจ๋ค.
Job์ด ์คํจํ๋ฉด ๊ฐ์ exception์ผ๋ก ๋ถ๋ชจ๋ ์ทจ์๋๋ค. Job์ ์ทจ์๋ ๋ถ๋ชจ๋ฅผ ์ทจ์์ํค์ง ์๋๋ค. ๋ถ๋ชจ๋ ์ด๋ ๊ฒ ์๋ ๋ค์ ์(์ฌ๊ท์ ์ผ๋ก ๋ชจ๋ ์๋ ๋ค์) ์ทจ์์ํฌ ์ ์๋ค.
๋์์ฑ๊ณผ ๋น๋๊ธฐ์ฑ
thread-safeํ๋ค
ํจ์
abstract suspend fun join(): Unit
job์ด ์๋ฃ๋ ๋๊น์ง ์ฝ๋ฃจํด์ ์ผ์์ ์์ํจ๋ค. Job์ด ์๋ฃ๋์์ ๋์ ์ฝ๋ฃจํด์ด ์์ง active ์ํ์ผ ๋ join()์ ํธ์ถํ๋ฉด job์ด ์ฌ์คํํ๋ค. job์ด new ์ํ์ผ ๋์๋ ์ฝ๋ฃจํด์ ์คํ์ํจ๋ค.
abstract fun cancel(cause: CancellationException? = null) : Unit
job์ ์ทจ์์ํจ๋ค. cause๋ ๋๋ฒ๊น ๋ชฉ์ ์ผ๋ก ์ทจ์ ์ด์ ๋ฅผ ์์ธํ๊ฒ ์๋ฌ๋ฉ์์ง๋ก ๋จ๊ธฐ๊ณ ์ถ์ ๋ ์ฌ์ฉํ ์ ์๋ค.
fun Job.cancel(message: String, casue: Throwable? = null) : Unit
ํ์ฌ Job์ ์ทจ์์ํจ๋ค.
fun CoroutineContext.cancel(cause: CancellationException? = null): Unit
์ด context ์์ Job์ ์ทจ์์ํจ๋ค.
suspend fun Job.cancelAndJoin(): Unit
Job์ ์ทจ์์ํค๊ณ ์ทจ์๋ Job์ด ์๋ฃ๋ ๋๊น์ง ์คํํ๋ ์ฝ๋ฃจํด์ ์ผ์์ค์ง์ํจ๋ค.