본문 바로가기

Job

[코루틴] 일시 중단 함수 - suspend 일시 중단 함수1. suspend fun 키워드로 선언되고 함수 내에 일시 중단 지점을 포함하는 함수2. 일시 중단 지점이 포함된 코드를 재사용이 가능한 단위로 추출3. 코루틴 내부에서 실행되는 코드의 집합일 뿐 코루틴이 아니다 fun main() = runBlocking { delay(1000L) println("Hello World") delay(1000L) println("Hello World")}// 결과Hello WorldHello World 일시 중단 함수를 사용해 위 코드를 수정suspend fun delayAndPrint() { delay(1000L) println("Hello World")}fun main() = runBlocking { val sta.. 더보기
[코루틴] 구조화된 동시성 구조화된 동시성 (Structured Concurrency)비동기 작업을 구조화함으로써 프로그래밍을 안정적이고 예측할 수 있게 만드는 원칙  코루틴의 구조화된 동시성 특성Parent - Child 관계fun main() = runBlocking { launch { // 부모 코루틴 launch { } // 자식 코루틴 }}부모 코루틴의 실행 환경(Context)가 자식 코루틴에게 상속Job을 제어하는데 사용부모 코루틴이 취소되면 자식 코루틴도 취소부모 코루틴은 자식 코루틴이 완료될 때까지 대기CoroutineScope을 사용해 코루틴이 실행되는 범위 제한 가능   1. 실행 환경 상속기본적으로 부모 코루틴이 자식 코루틴을 생성하면 Parent CoroutineContext ->.. 더보기
[코루틴] CoroutineContext CoroutineContext란?public fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit): Jobpublic fun CoroutineScope.async( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Deferred 코.. 더보기
[코루틴] async & Deferred async란?- 코루틴 빌더- launch 빌더와 비슷, async는 결과값을 담기 위해 Deferred를 반환// async. Deferred 반환public fun CoroutineScope.async( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Deferred { val newContext = newCoroutineContext(context) val coroutine = if (start.isLazy) LazyDeferredCoroutine(newCont.. 더보기