Skip to content

Commit

Permalink
Update Kotlin-Coroutine-Concepts.md
Browse files Browse the repository at this point in the history
  • Loading branch information
HomoEfficio authored May 7, 2021
1 parent 5d787c8 commit 0cb167d
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions Kotlin-Coroutine-Concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
일반적인 프로그래밍 용어로서의 코루틴은 스레드, light-weight 스레드와는 아무런 관계가 없다.


## Coroutine
# Coroutine

>A coroutine is an instance of suspenable computation.
>
>_from https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine_
>_from https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
코루틴은 코드 블록을 받아서 다른 코드와 동시에 실행된다는 점에서는 개념적으로 스레드와 비슷하지만,
코루틴은 어떤 특정 스레드에 묶이지 않는다.
Expand All @@ -34,9 +34,9 @@
코루틴은 실행 흐름이면서도 스레드보다 가볍다는 관점에서 light-weight 스레드라고 할 수 있지만 스레드와는 많이 다르다.


### Coroutine Builder
## Coroutine Builder

- 주어진 스코프 안에서 새 코루틴을 만드는 함수
- 주어진 스코프(바로 아래 'CoroutineScope' 참고) 안에서 새 코루틴을 만드는 함수
- 따라서 이미 존재하는 스코프 안에서만 호출 가능
- 모든 코루틴 빌더 함수는 CoroutineScope의 확장 함수이며
- `scopeA.launch``scopeA` 스코프 안에서 실행될 수 있는 새 코루틴을 생성
Expand All @@ -45,44 +45,57 @@

### fun runBlocking

- https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html
- GlobalScope에서 실행될 수 있는 코루틴 생성

### fun launch

- https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html
- `Job` 반환

### fun async

- https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html
- `Deferred<T>` 반환



## CoroutineScope
# CoroutineScope

- https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html
- 코루틴이 실행될 수 있는 영역/범위
- 코루틴은 스코프 내에서만 실행될 수 있고, 아무런 스코프가 없는 곳에서는 실행될 수 없다
- 모든 스코프는 결국 GlobalScope를 뿌리로 해서 생겨나며 따라서 모든 스코프는 GlobalScope의 하위 스코프다
- 하위 스코프에서 생성된 코루틴이 완료되기 전에는 상위 스코프의 코루틴도 완료될 수 없다
- CoroutineContext는 CoroutineScope의 property 속성이다

### Scope Builder
## Scope Builder

- 새로운 스코프를 생성하는 suspend 함수
- 새로운 스코프를 생성하는 suspend 함수(Scoping Function이라고 부르기도 한다)
- 새로운 스코프를 만들지만 새로운 코루틴을 만들지는 않는다
- suspend 함수이므로 코루틴 내에서만 호출 가능
- suspend 함수이므로 코루틴 안에서만 호출 가능

### Scope Buillder와 Coroutine Builder
- 코루틴을 만드는 코루틴 빌더 함수는 스코프 안에서만 호출 가능한데, 스코프를 만드는 스코프 빌더 함수는 코루틴 안에서만 호출 가능하다고 하니 이 부분이 순환 논리 같아 혼동스럽다
- 일반적으로 GlobalScope의 코루틴 빌더 함수를 호출(`GlobalScope.launch(또는 .async, .runBlocking, ...)`)해서 글로벌 스코프에서 코루틴 A를 만들고,
- 글로벌 스코프에서 만든 코루틴 A 안에서 suspend 함수인 스코프 빌더 함수를 호출(`coroutineScope { ... }`, `withContext(context) { ... }`)해서 글로벌 스코프 하위의 스코프 B를 만들고,
- 이 B 스코프 안에서 다른 suspend 함수를 호출하는 방식으로 사용한다

### suspend fun coroutineScope

- https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html
- 기존 스코프에 있는 컨텍스트를 상속받고, 기존 컨텍스트의 Job은 오버라이드하면서 새 스코프 생성

### suspend fun withContext

- https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/with-context.html
- 특정 컨텍스트를 가지는 새 스코프 생성



## CoroutineContext
# CoroutineContext

- https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/-coroutine-context/
- CoroutineScope의 property로서 스코프와 생명주기를 함께 한다
- 스코프 안에 있는 코루틴(들)이 스코프 안에서 전역적으로 사용될 수 있는 문맥(정보 및 함수 저장소)
- 컨텍스트 내용 중 중요한 것은 `Job``Dispatcher`
Expand All @@ -95,37 +108,43 @@



## Coroutine Dispatcher
# Coroutine Dispatcher

- 코루틴이 어느 스레드에서 실행/재개될지 지정

### Dispatchers.Default
## Dispatchers.Default

- 명시적으로 지정하지 않으면 사용되는 디스패처
- common pool of shared background threads에서 실행/재개
- CPU를 많이 소모하는 연산 집중 코루틴에 적합

### Dispatchers.IO
## Dispatchers.IO

- shared pool of on-demand created threads에서 실행/재개
- File I/O, blocking socket I/O 처럼 블로킹 연산에 적합

### Dispatchers.Unconfined
## Dispatchers.Unconfined

- 코루틴 컨텍스트를 생성하는 현재 실행 중인 스레드에서 실행
- 재개될 때는 특정 스레드나 풀이 아니라 해당 suspend 함수가 사용하는 어떤 스레드에서도 재개될 수 있음
- **일반적인 코드에서는 사용하지 말아야 한다**
- see [here](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html)
- 이 디스패처를 사용해서 생성되는 중첩 코루틴은 스택 오버플우를 피하기 위해 이벤트루프를 형성

### Dispatchers.Main
## Dispatchers.Main

- UI 객체가 사용되는 main 스레드에서 실행/재개
- 보통 싱글 스레드 환경에서 사용



## Job
# Continuation

TODO



# Job

TODO

Expand Down

0 comments on commit 0cb167d

Please sign in to comment.