Kotlin (Android)
Coroutine 정리
Gemstone
2022. 6. 7. 14:33
왜 쓰레드를 안쓰고 코루틴을 사용할까?
쓰레드는 resource intesive하다.
그러나 코루틴은 lightweight 쓰레드이다. (쓰레드 풀에서 동작)
async 코드를 단순화하고, 콜백과 동기화가 되게 간편하다.
또한 문법도 단순하다. (simple syntax)
또 언제든 멈추고 재개할 수 있다.
# 코루틴 - basic concepts
Scope - create and run coroutines, provides lifecycle events
Context - the Scope provides a context in which the coroutine runs (state of coroutine)
Suspending functions - functions that can be run in a coroutine (can be suspended)
Job - a handle on a coroutine
Deferred - a future result of a coroutine
Dispatcher - manages which thread(s) the coroutine runs on
Error handling