coroutine은 경량 thread 이기 때문에 coroutine 내부에서 자식 coroutine에 에러가 생겼을 경우 별도의 Exception Handler를 설정해주지않으면 자식 coroutine은 부모 coroutine까지 취소 시킨다.

→ 이로 인한 문제점을 방지하기 위해 에러 전파 방향을 자식으로 한정짓는 것이 바로 SuperVisor Job이다.

⭐️ Coroutine 예시

Parent Coroutine

Child Coroutine1

Child Coroutine2

진행 상황

  1. Child Coroutine1이 에러로 인해 취소
  2. Parent Coroutine으로 취소 전파
  3. Childe Coroutine2에 취소 전파

⭐️ Coroutine 예제

suspend fun main() {
        CoroutineScope(Dispatchers.IO).launch {
            val firstChildJob = launch(Dispatchers.IO) {
                throw AssertionError("에러로 인해 firstChildJob 취소")
            }

            val secondChildJob = launch(Dispatchers.Default) {
                delay(1000)
                println("secondChildJob 동작 중...")
            }

            firstChildJob.join()
            secondChildJob.join()
        }.join()
    }

스크린샷 2023-07-20 오전 10.11.18.png

💙 SuperVisor Job Coroutine 예시

진행 상황