Skip to content

Latest commit

ย 

History

History
146 lines (121 loc) ยท 8.11 KB

context.md

File metadata and controls

146 lines (121 loc) ยท 8.11 KB

Context

Dispatchers

  • Dispatcher.Default : CoroutineContext๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์•˜์„๋•Œ ์ง€์ •๋˜๋Š” ๊ธฐ๋ณธ Dispatcher
    • launch, async๊ณผ ๊ฐ™์€ ํ‘œ์ค€ Builder์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ Dispatcher
    • JVM์˜ ๊ณต์œ ๋œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์Šค๋ ˆ๋“œ์˜ commonPool์—์„œ ๋™์ž‘
  • Dispatcher.IO : I/O ์ž‘์—…์„ ์œ„ํ•œ Dispatcher
  • Dispatcher.Main : ๋ฉ”์ธ์Šค๋ ˆ๋“œ(UI)์— ๊ตญํ•œ๋œ Dispatcher
  • Dispatcher.Unconfined : ์ฒซ๋ฒˆ์งธ ์ค‘๋‹จ ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ๋งŒ๋‚ ๋•Œ ๊นŒ์ง€ ํ˜ธ์ถœ ์Šค๋ ˆ๋“œ์—์„œ Coroutine์„ ์‹œ์ž‘ํ•˜์—ฌ ์ค‘๋‹จ ์ดํ›„์— ์žฌ๊ฐœ(resume)๋  ๋•Œ๋Š” ์ค‘๋‹จ ํ•จ์ˆ˜๋ฅผ ์žฌ๊ฐœํ•œ ์Šค๋ ˆ๋“œ์—์„œ ์ˆ˜ํ–‰๋œ๋‹ค.
    • Coroutine์ด CPU ์‹œ๊ฐ„์„ ์†Œ๋ชจํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ๊ณต์œ ๋˜๋Š” ๋ฐ์ดํ„ฐ(UI)๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์ฒ˜๋Ÿผ ํŠน์ • ์Šค๋ ˆ๋“œ์— ๊ตญํ•œ๋œ ์ž‘์—…์ด ์•„๋‹Œ ๊ฒฝ์šฐ ์ ์ ˆํ•˜๋‹ค.

Scope

GlobalScope

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋™์ž‘ํ•˜๋Š”๋™์•ˆ ๋ณ„๋„์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ด€๋ฆฌํ•˜์ง€์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Scope๋กœ
์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ์ด ์ฒ˜์Œ ์‹œ์ž‘ํ• ๋•Œ๋ถ€ํ„ฐ ์ข…๋ฃŒํ•  ๋•Œ ๊นŒ์ง€ ํ•˜๋‚˜์˜ CoroutineContext์•ˆ์—์„œ ๋™์ž‘ํ•œ๋‹ค.
Dispatchers.Default์˜ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

CoroutineScope

ํŠน์ •ํ•œ ๋ชฉ์ ์˜ Dispatcher๋ฅผ ์ง€์ •ํ•˜์—ฌ ์ƒˆ๋กœ์šด Coroutine์˜ ๋ฒ”์œ„๋ฅผ ์ •์˜ํ•œ๋‹ค.
๋ชจ๋“  Coroutine Builder(launch, async)์˜ ํ™•์žฅ์ด๋‹ค.

ProducerScope

Builder

  • launch
    • launch{}์˜ ์‹คํ–‰์œผ๋กœ Job์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค
    • ๊ฒฐ๊ณผ๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ์ง€์•Š๋‹ค
    • ๊ทธ์ž๋ฆฌ์—์„œ ๋ฐ”๋กœ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค
    • join()์„ ๋งŒ๋‚˜๋ฉด ํ˜„์žฌ Coroutine์ด ์™„๋ฃŒ๋Œˆ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค (suspend ๋œ๋‹ค)
    • CoroutineScope๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค
  • async
    • Deffered๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
    • ๊ฒฐ๊ณผ๋ฅผ๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ๋‹ค
    • await()๋ฅผ ๋งŒ๋‚˜๋ฉด ํ˜„์žฌ Coroutine์ด ์™„๋ฃŒ๋Œˆ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค (suspend ๋œ๋‹ค)
    • CoroutineScope๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค
  • produce
    • ReceiveChannel์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค
    • producer-consumerํŒจํ„ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋นŒ๋”
    • ProducerScope๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค
  • runBlocking
    • Coroutine์„ ์ƒ์„ฑํ•œ ํ›„ Coroutine์ด ์™„๋ฃŒ๋˜์–ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ• ๋•Œ๊นŒ์ง€ ํ˜„์žฌ Thread๋ฅผ Blockํ•œ๋‹ค
    • runBlocking์˜ ๊ธฐ๋ณธ Dispatcher๋Š” ์‹œ์ž‘๋œ Threa์ด๋‹ค
    • CoroutineScope๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

CoroutineStart

launch,async๋“ฑ์˜ Builder ํ•จ์ˆ˜์—์„œ start ๋งค๊ฐœ๋ณ€์ˆ˜์— ์‚ฌ์šฉ๋œ๋‹ค. coroutine์˜ ์‹œ์ž‘์— ๊ด€๋ จํ•˜์—ฌ ์„ค์ •ํ•œ๋‹ค

  • CoroutineStart.DEFAULT: CoroutineContext์— ๋”ฐ๋ผ ์ฆ‰์‹œ Coroutine์„ ์‹œ์ž‘ํ•œ๋‹ค
  • CoroutineStart.LAZY: start()ํ•จ์ˆ˜๋˜๋Š” await() ์‚ฌ์šฉํ•˜์—ฌ ์‹œ์ž‘๋ ๋•Œ๊นŒ์ง€ ์ž‘์—…์„ ์ง€์—ฐํ•œ๋‹ค
  • CoroutineStart.ATOMIC: ์›์ž์ ์œผ๋กœ(์ฆ‰,์ทจ์†Œํ•  ์ˆ˜ ์—†๋Š” ๋ฐฉ์‹์œผ๋กœ) CoroutineContext์— ๋”ฐ๋ผ ์‹คํ–‰๋˜๋„๋ก Coroutine์„ ์˜ˆ์•ฝํ•œ๋‹ค.DEFAULT์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ์‹คํ–‰์„ ์‹œ์ž‘ํ•˜๊ธฐ์ „์— Coroutine์„ cancel() ํ•  ์ˆ˜ ์—†๋‹ค
  • CoroutineStart.UNDISPATCHED: Coroutine์ด Dispatchers.Unconfined๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ์ž‘๋œ ๊ฒƒ์ฒ˜๋Ÿผ ํ˜„์žฌ์Šค๋ ˆ๋“œ์—์„œ ์ฒซ๋ฒˆ์งธ ์ค‘๋‹จ์ (suspend)๊นŒ์ง€ Coroutine์„ ์‹คํ–‰ํ•œ๋‹ค. Coroutine์ด ์ค‘๋‹จ(suspend)์ด ์žฌ๊ฐœ๋˜์—ˆ์„๋•Œ์˜ ์Šค๋ ˆ๋“œ์—์„œ ๋‹ค์‹œ ์ˆ˜ํ–‰๋œ๋‹ค

Job

  • ์ˆ˜๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ ์™„๋ฃŒ๋กœ ๋๋‚œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ทจ์†Œํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ƒ์œ„-ํ•˜์œ„ ๊ณ„์ธต๊ตฌ์กฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ƒ์œ„ Job์„ ์ทจ์†Œํ•˜๋ฉด ํ•˜์œ„Job์ด ์ฆ‰์‹œ ์ทจ์†Œ๋œ๋‹ค.

State

Job์€ Coroutine์˜ 6๊ฐ€์ง€ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋‹ค. Job์œผ๋กœ Coroutine์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ  ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค.

state isActive isCompleted isCancelled
New(optional init state) false false false
Active(default init state) true false false
Completing(transient state) true false false
Cancelling(transient state) false false true
Cancelled(final state) false true true
Completed(final state) false true false

Job LifeCycle

image

Functions

  • start : ์•„์ง ์‹œ์ž‘๋˜์ง€ ์•Š์€ Job์ด ์žˆ๋Š”๊ฒฝ์šฐ ๊ด€๋ จํ•œ Coroutine์„ ์‹œ์ž‘ํ•œ๋‹ค
    • ๋™์ž‘์ค‘์ธ๊ฒฝ์šฐ true, ์ค€๋น„ ๋˜๋Š” ์™„๋ฃŒ์ƒํƒœ์ด๋ฉด false๋ฅผ returnํ•œ๋‹ค
  • join : ํ˜„์žฌ ๋™์ž‘์ค‘์ธ Coroutine ๋™์ž‘์ด ๋๋‚ ๋Œ€ ๊นŒ์ง€ ๋Œ€๊ธฐํ•œ๋‹ค
    • async{} await์™€ ๋น„์Šทํ•œ ๊ฐœ๋…
  • cancel : Job์„ ์ทจ์†Œ
  • cancelAndJoin : ํ˜„์žฌ Job์„ ์ทจ์†Œํ•˜๊ณ  ์ทจ์†Œ๋œ ์ž‘์—…์ด ์™„๋ฃŒ๋ ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•œ๋‹ค.
  • cancelChildren : ํ˜„์žฌ Coroutine์˜ ํ•˜์œ„ Job์„ ๋ชจ๋‘ ์ทจ์†Œํ•œ๋‹ค
  • ensureActive : ํ˜„์žฌ Job์ด ํ™œ์„ฑ ์ƒํƒœ์ธ์ง€ ํ™•์ธ
  • invokeOnCompletion : Job์— ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋“ฑ๋กํ•˜๊ณ  ์™„๋ฃŒ ๋˜๋Š” ์ทจ์†Œ๋ ๋•Œ ํ•ธ๋“ค๋Ÿฌ ํ˜ธ์ถœ

Inheritors

  • CompletableJob : complete()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Job์„ ์™„๋ฃŒ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • SupervisorJob : children Job๋“ค์ค‘ ํ•˜๋‚˜๊ฐ€ ์‹คํŒจํ•˜๋”๋ผ๋„ ๋‹ค๋ฅธ children Job์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.
  • Deferred : async{}์—์„œ ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ๋Š” Job
  • NonCancellable : ํ•ญ์ƒ ํ™œ์„ฑ์ƒํƒœ์ธ ์ทจ์†Œ ๋ถˆ๊ฐ€๋Šฅํ•œ Job

Channel

Channel์€ ๊ฐœ๋…์ ์œผ๋กœ BlockingQueue์™€ ์œ ์‚ฌํ•˜๋‹ค.
BlockingQueue์—์„œ put์„ ํ• ๋•Œ ๊ฐ€๋“ ์ฐจ์žˆ๋‹ค๋ฉด Blockํ•˜์—ฌ ๋Œ€๊ธฐํ•˜๊ณ , take๋ฅผ ํ• ๋•Œ ๋น„์–ด์žˆ๋‹ค๋ฉด Blockํ•˜์—ฌ ๋Œ€๊ธฐํ•œ๋‹ค Channel์€ ๋น„์Šทํ•˜๊ฒŒ send์™€ receive๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ coroutine ์‚ฌ์ด์— ์ˆœ์„œ๋ฅผ ๋ณด์žฅ๋ฐ›์œผ๋ฉฐ ๊ณต์œ ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
๋˜ํ•œ Channel์€ Queue์™€ ๋‹ฌ๋ฆฌ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„๋•Œ close ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
Channel์ด close๋˜์—ˆ์„ ๋•Œ, close์ „๊นŒ์ง€ sendํ–ˆ๋˜ Element๋“ค์€ ๋‚จ์•„์žˆ๋‹ค.

Functions

  • send : Element๋ฅผ ์ฑ„๋„๋กœ ์ „์†ก. ์ฑ„๋„์˜ ๋ฒ„ํผ๊ฐ€ ๊ฐ€๋“์ฐจ ์žˆ๋‹ค๋ฉด Blockํ•˜์—ฌ ๋Œ€๊ธฐ
  • receive : Element๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ์‚ฌ์šฉ. ์ฑ„๋„์ด ๋น„์–ด์žˆ๋‹ค๋ฉด Blockํ•˜์—ฌ ๋Œ€๊ธฐ
  • cancel : ์ฑ„๋„์˜ ๋‚˜๋จธ์ง€ Element ์ˆ˜์‹ ์„ ์ทจ์†Œํ•œ๋‹ค. ์ฑ„๋„์„ ๋‹ซ๊ณ  ๋ฒ„ํผ๋ง๋œ ๋ชจ๋“  Element๋“ค์„ ์ œ๊ฑฐํ•œ๋‹ค
  • close : ์ฑ„๋„์„ ๋‹ซ์ง€๋งŒ, ๋ฒ„ํผ๋ง๋œ ์š”์†Œ๋“ค์€ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค
  • invokeOnClose : ์ฑ„๋„์ด ๋‹ซํžˆ๊ฑฐ๋‚˜ ์ทจ์†Œ๋ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋“ฑ๋กํ•œ๋‹ค
  • offer : ์ฑ„๋„์— Element๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€๋งŒ coroutine์„ suspendํ•˜์ง€์•Š๋Š”๋‹ค
  • poll : ์ฑ„๋„์—์„œ Element๋ฅผ ๋ฐ›์ง€๋งŒ coroutine์„ suspendํ•˜์ง€์•Š๋Š”๋‹ค

Capacity Policy

  • RENDEVOUS
    • ๊ธฐ๋ณธ์šฉ๋Ÿ‰์ •์ฑ…
    • ๋ฒ„ํผ๊ฐ€ ์—†์Œ image
  • BUFFERED
    • ์ฑ„๋„์— ์šฉ๋Ÿ‰์„ ๋ถ€์—ฌ
    • ๋ฒ„ํผ์˜ ์šฉ๋Ÿ‰์ด ๊ฐ€๋“์ฐจ๊ฒŒ๋˜๋ฉด image
  • UNLIMITED
    • ์ฑ„๋„์— ๋ฌด์ œํ•œ ์šฉ๋Ÿ‰์„ ๋ถ€์—ฌ (๋ฉ”๋ชจ๋ฆฌ ๊ณ ๊ฐˆ์‹œ ๊นŒ์ง€)
    • Coroutine์„ suspend ํ•˜์ง€์•Š๊ณ  ๋ชจ๋“  Element๋ฅผ ์ฑ„๋„์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค image
  • CONFLATED
    • send์‹œ suspendํ•˜์ง€์•Š๋Š”๋‹ค
    • receive์‹œ์— ๊ฐ€์žฅ ์ตœ์‹ ์˜ Element๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค image

Top-Level Suspending Functions

  • delay : ์Šค๋ ˆ๋“œ๋ฅผ ์ฐจ๋‹จํ•˜์ง€์•Š๊ณ  Coroutine์„ ์ฃผ์–ด์ง„ ์‹œ๊ฐ„๋™์•ˆ ์ง€์—ฐํ•œ๋’ค ๋‹ค์‹œ ์‹œ์ž‘
  • yeild : ํ˜„์žฌ Coroutine์˜ Dispatcher์˜ Thread๋ฅผ ๊ฐ€๋Šฅํ•œ๊ฒฝ์šฐ ๋‹ค๋ฅธ Coroutine์— ์–‘๋ณดํ•œ๋‹ค
  • withContext : ์ง€์ •๋œ CoroutineContext๋ฅผ ๋‹ค๋ฅธ CoroutineContext๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค
    • CoroutineContext์˜ ์ƒˆ Dispatcher๊ฐ€ ์ง€์ •๋œ ๊ฒฝ์šฐ์—๋Š” block์‹คํ–‰์„ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋กœ ์ด๋™ํ•˜๊ณ  ์™„๋ฃŒ๋ ์‹œ์— ์›๋ž˜ Dispatcher๋กœ ๋Œ์•„๊ฐ„๋‹ค
  • withTimeout : ์ง€์ •๋œ ์ œํ•œ์‹œ๊ฐ„๋‚ด์— block๋‚ด์˜ Coroutine์ด ์™„๋ฃŒ๋˜์ง€์•Š์„๊ฒฝ์šฐ TimeoutCancellationException์„ throwํ•œ๋‹ค
  • withTimoeoutOrNull : ์ง€์ •๋œ ์ œํ•œ์‹œ๊ฐ„๋‚ด์— block๋‚ด์˜ Coroutine์ด ์™„๋ฃŒ๋˜์ง€์•Š์„๊ฒฝ์šฐ null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค
  • awaitAll : ์ฃผ์–ด์ง„ ๋ชจ๋“  ์ž‘์—…์˜ ์™„๋ฃŒ๋ฅผ ๊ธฐ๋‹ค๋ฆฐ๋‹ค
    • ์ฃผ์–ด์ง„ ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋ ๋•Œ๊นŒ์ง€ ํ˜„์žฌ Coroutine์„ suspendํ•œ๋‹ค
    • e.g. async{}์˜ ์™„๋ฃŒ ๋Œ€๊ธฐ
  • joinAll : ์ฃผ์–ด์ง„ ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค
    • ์ฃผ์–ด์ง„ ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋ ๋•Œ๊นŒ์ง€ ํ˜„์žฌ Coroutine์„ suspendํ•œ๋‹ค
    • e.g. launch{}์˜ ์™„๋ฃŒ ๋Œ€๊ธฐ