Coroutine์˜ IO vs Default Dispatcher

๐Ÿ’กCoroutineDispatcher์—์„œ IO์™€ Default์˜ ์ฐจ์ด์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

CoroutineDispatcher

  • ์ฝ”๋ฃจํ‹ด ๋””์ŠคํŒจ์ฒ˜๋Š” ์ฝ”๋ฃจํ‹ด์„ ์Šค๋ ˆ๋“œ๋กœ ๋ณด๋‚ด๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์ž‘์—…์€ ์Šค๋ ˆ๋“œ ์œ„์—์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๊ณ , ์ฝ”๋ฃจํ‹ด ๋˜์•ˆ ์ž‘์—…์ด๋ฏ€๋กœ ์Šค๋ ˆ๋“œ ์œ„์—์„œ๋งŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Coroutine์€ ์Šค๋ ˆ๋“œํ’€์„ ๋งŒ๋“ค๊ณ  Dispatcher๋Š” Coroutine์„ ์ ์ ˆํ•œ Thread์— ๋ณด๋‚ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐฐ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.

CoroutineDispatcher

 

CoroutineDispatcher

CoroutineDispatcher Base class to be extended by all coroutine dispatcher implementations. If kotlinx-coroutines is used, it is recommended to avoid ContinuationInterceptor instances that are not CoroutineDispatcher implementations, as CoroutineDispatcher

kotlinlang.org

 

Dispathcer์˜ ์ข…๋ฅ˜

  • CoroutineDispatcher์—๋Š” Main, IO, Default๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ผ๋ฐ˜์ ์œผ๋กœ IO Dispatcher์˜ ๊ฒฝ์šฐ Difault Dispatcher์— ๋น„ํ•ด ํ‰๊ท  1~2์ดˆ ์ •๋„ ๋น ๋ฅธ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

Dispatcher.Main

  • UI์™€ ์ง์ ‘ ์ƒํ˜ธ ์ž‘์šฉ์„ ํ•˜๋Š” ์ž‘์—…์„ ๋ฉ”์ธ Thread์— ์ „๋‹ฌํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฉ”์ธ(UI) ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰๋˜๋Š” ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, UI์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ฑฐ๋‚˜ UI ๊ด€๋ จ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ๋น„๋™๊ธฐ ์ž‘์—…์„ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰ํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ๋ฌด๊ฑฐ์šด ์ž‘์—…์ด ์•„๋‹Œ ๊ฒฝ๋Ÿ‰ ์ž‘์—…์— ์ดˆ์ฒจ์„ ๋งž์ถฐ์•ผํ•˜๋ฉฐ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ANR ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Dispacher.IO

  • ๋„คํŠธ์›Œํฌ, DB ์ž…์ถœ๋ ฅ์ด ์žˆ๋Š” ์ž‘์—…๋“ค์— ๋Œ€ํ•ด ์ ์ ˆํ•œ Thread๋กœ ํ• ๋‹นํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ตœ๋Œ€ 64๊ฐœ์˜ Thread๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํ• ๋‹น์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • Default Dispatcher์™€ ์Šค๋ ˆ๋“œ๋ฅผ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— switching์œผ๋กœ ์ธํ•œ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ผ์œผํ‚ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Dispatcher.Default

  • ์—ฐ์†์ ์ธ ์ž‘์—…, CPU๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์ž‘์—… ๋“ฑ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ ์ ˆํ•œ Thread๋กœ ํ• ๋‹นํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • ์Šค๋ ˆ๋“œ ํ’€์—์„œ ๋ณดํ†ต CPU Core์˜ ๊ฐœ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” Thread๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํ• ๋‹น์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ์ด ๋””์ŠคํŒจ์ฒ˜๋Š” JVM์˜ ๊ณต์œ  ์Šค๋ ˆ๋“œํ’€์„ ์‚ฌ์šฉํ•˜๊ณ , ๋™์‹œ ์ž‘์—… ๊ฐ€๋Šฅ ์ตœ๋Œ€ ๊ฐฏ์ˆ˜๋Š” CPU์˜ ์ฝ”์–ด ์ˆ˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • 4์ฝ”์–ด CPU์˜ ๊ฒฝ์šฐ ์ตœ๋Œ€ 4๊ฐœ์˜ ๋ณ‘๋ ฌ ์ž‘์—…

๋ณดํ†ต ํ•˜๋‚˜์˜ Core์—์„œ ํ•˜๋‚˜์˜ Thread๋ฅผ ์ฒ˜๋ฆฌํ–ˆ์ง€๋งŒ, ์š”์ฆ˜์—” 2๊ฐœ์˜ Thread๋ฅผ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

Dispacher.IO vs Dispatcher.Default

  • ๋‘ ๋””์ŠคํŒจ์ฒ˜๋Š” ์ฝ”๋ฃจํ‹ด์—์„œ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์Šค๋ ˆ๋“œํ’€์„ ๋‹ค๋ฃจ๋ฉฐ, ์„œ๋กœ ๋‹ค๋ฅธ ๋ชฉ์ ์— ๋งž๊ฒŒ ์ตœ์ ํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

Dispacher.IO

  • I/O ๋ฐ”์šด๋“œ ์ž‘์—…, ํŒŒ์ผ ์ž…์ถœ๋ ฅ, ๋„คํŠธ์›Œํฌ ๋“ฑ์— ์ตœ์ ํ™”๋œ ๋””์ŠคํŒจ์ฒ˜ ์ž…๋‹ˆ๋‹ค.
  • ๋Œ€๊ทœ๋ชจ ์Šค๋ ˆ๋“œ ํ’€์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋™์‹œ ์ž‘์—… ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 64๊ฐœ ์ด์ƒ ํ—ˆ์šฉ๋ฉ๋‹ˆ๋‹ค.

Dispacher.Default

  • CPU ์ง‘์•ฝ์ ์ธ ์ž‘์—…์— ์ตœ์ ํ™”๋œ ๋””์ŠคํŒจ์ฒ˜์ž…๋‹ˆ๋‹ค.
  • ๊ธฐ๋ณธ์ ์œผ๋กœ CPU ๋…ผ๋ฆฌ์  ์ฝ”์–ด ์ˆ˜๋งŒํผ ์Šค๋ ˆ๋“œ๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.
Runtime.getRuntime().availableProcessors()
  • ๋ณต์žกํ•œ ๊ณ„์‚ฐ, ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, CPU๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์ž‘์—…์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.

์ •๋ฆฌ

  • Dispacher.Default๋Š” CPU ์ง‘์•ฝ์ ์ธ ์ž‘์—…์— ์ตœ์ ํ™”๋˜์–ด ์žˆ์œผ๋ฉฐ, CPU ์ฝ”์–ด ์ˆ˜๋งŒํผ์˜ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํšจ์œจ์ ์œผ๋กœ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • Dispacher.IO๋Š” I/O ๋ฐ”์šด๋“œ ์ž‘์—…์— ์ ํ•ฉํ•˜๋ฉฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ ๋งŽ์€ ์ˆ˜์˜ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ธ”๋กœํ‚น ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • Dispacher.Main์€ UI ๊ด€๋ จ ์ž‘์—…์„ ์•ˆ๋“œ๋กœ์ด๋“œ ๋ฉ”์ธ ์Šค๋ ˆ๋“œ์—์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ 

https://medium.com/@ans188/android-dispatcher-io์™€-default์˜-์ฐจ์ด-25871b54dc4d