반응형
Coroutine Builder (=코루틴 빌더)
: 코루틴을 생성할 때 사용하는 여러 함수들을 일컫는 말
공통적인 라이프사이클 및 context가 정해진 몇몇 작업 등 구체적인 영역 안에서만 코루틴을 적용하므로 대표적인 함수들은 아래와 같다.
Coroutine Builder 대표적 함수들(=코루틴 생성함수)
cf) coroutineScope, withContext 등은 CoroutineScope 함수이다.
1. launch { .. }
- 새 코루틴을 생성하고 바로 실행
- Job을 리턴함 cf) async
public fun CoroutineScope.launch(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job
2. async { .. }
- 새 코루틴을 생성하고 바로 실행
- Deferred<T>를 리턴함
public fun <T> CoroutineScope.async(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T
): Deferred<T>
3. . runBlocking { .. }
- 자식 스레드가 완료될 때 까지 현재 스레드를 block 시킨다.
(=내부의 모든 코루틴이 실행을 완료할 때까지 호출하는 동안 runBlocking스레드를 실행하는 스레드(이 경우 메인 스레드)가 차단됨)
@Throws(InterruptedException::class)
public actual fun <T> runBlocking(context: CoroutineContext, block: suspend CoroutineScope.() -> T): T
[참조]
- https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
- https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/
-
- https://medium.com/androiddevelopers/coroutines-first-things-first-e6187bf3bb21
- https://kt.academy/article/cc-scoping-functions
반응형