분류 전체보기
-
[Coroutine] 코루틴 학습 - 5 (Coroutine context)Java/Kotlin 2022. 4. 28. 21:06
Coroutine context coroutine builder들의 정의를 살펴보면 첫 번째 파라미터에 CoroutineContext 타입을 넘겨받고 있는 것을 확인할 수 있다. public fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit ): Job launch()의 리시버와 마지막 파라미터의 리시버는 CoroutineScope이다. CoroutineScope는 CoroutineContext를 래핑하고 있는 인터페이스이다. public interface C..
-
[Coroutine] 코루틴 학습 - 4 (Structured Concurrency)Java/Kotlin 2022. 4. 27. 09:37
Structured Concurrency 지난 포스트에서 살펴봤던 launch builder에서 GlobalScope는 코루틴이 완료되지 않아도 기다리지 않고 프로세스가 종료되었다. (코루틴은 쓰레드를 블로킹하지 않기 때문) 아래의 코드는 "World!"를 출력할 때까지 기다리지 않고 "Hello,"를 출력한 후에 바로 종료된다. fun main() = runBlocking { GlobalScope.launch { delay(1000L) println("World!") } println("Hello~") } // Hello~ public fun runBlocking( context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineSc..
-
[Coroutine] 코루틴 학습 - 3 (Coroutine Builders)Java/Kotlin 2022. 4. 27. 08:16
Coroutine builders 일시중단 함수는 다른 일시중단 함수을 호출할 때 continuation 객체를 넘겨야 한다. 일시중단 함수 내에서 일반 함수를 호출할 수 있다. 일반 함수는 일시중단 함수를 호출할 수 없다. 즉, 모든 일시중단 함수는 다른 일시중단 함수에 의해 호출되어야 한다. 그렇다면 이 일시중단 함수를 최초에 호출할 때 누군가는 호출을 해야할텐데, 어디에서 호출이 가능할까? coroutine builder를 이용하면 일반 함수에서 일시중단 함수를 호출할 수 있도록 징검다리 역할을 해준다. kotlinx.coruotines 라이브러리에서 세 가지 coroutine builder를 지원한다. launch runBlocking async launch builder launch는 개념상 새..
-
[Coroutine] 코루틴 학습 - 2 (Under the hood)Java/Kotlin 2022. 4. 23. 16:58
Under the hood Coroutine 일시중단 함수는 함수의 실행과 실행 후에도 실행 가능한 상태 머신과 같다. 상태를 식별하는 번호와 로컬 데이터는 continuation 객체에 저장한다. 한 함수의 Contunuation 다른 함수의 Continuation을 데코레이트 시키고, 결과적으로 모든 continuation은 다시 시작될 때 사용하는 콜스택을 나타낸다. Continuation-passing style 일시중단 함수를 구현 시, Continuation은 함수에서 함수로 마지막 argument로 전달된다. suspend fun getUser(): User? suspend fun setUesr(uesr: User) suspend fun checkAvailability(flight: Flig..