coroutine은 경량 thread 이기 때문에 coroutine 내부에서 자식 coroutine에 에러가 생겼을 경우 별도의 Exception Handler를 설정해주지않으면 자식 coroutine은 부모 coroutine까지 취소 시킨다.
→ 이로 인한 문제점을 방지하기 위해 에러 전파 방향을 자식으로 한정짓는 것이 바로 SuperVisor Job이다.
⭐️ Coroutine 예시
Parent Coroutine
Child Coroutine1
Child 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()
}
💙 SuperVisor Job Coroutine 예시
진행 상황