๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿค2024 ์•ˆ๋“œ๋กœ์ด๋“œ/๊ธฐ์ˆ™์‚ฌ ํ”„๋กœ์ ํŠธ ๊ธฐ๋ก

kotlin coroutines basic ๊ณต์‹๋ฌธ์„œ ์ฝ์–ด๋ณด๊ธฐ

by hyeonha 2024. 1. 17.

๋ชฉ์ฐจ

     

    ์ฝ”ํˆฌํ‹ด์€ ์ผ์‹œ์ ์œผ๋กœ ์‹คํ–‰์„ ์ค‘๋‹จํ•˜๊ณ  ๋‹ค์‹œ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ„์‚ฐ์˜ ์ธ์Šคํ„ด์Šค์ด๋‹ค.

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

     

    ์Šค๋ ˆ๋“œ๋Š” ๋™์‹œ์— ์—ฌ๋Ÿฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ์—์„œ ๋ฌด์–ธ๊ฐ€๋ฅผ ๊ธฐ๋‹ค๋ฆด ๋•Œ ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์Šค๋ ˆ๋“œ๋Š” ๋น„์šฉ์ด ํฐ ์ž‘์—…์ด๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ์Šค๋ ˆ๋“œ ์‚ฌ์šฉ ์‹œ ์„ฑ๋Šฅ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ!

     

    ์ฝ”๋ฃจ์นœ์€ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ.

    ์ฝ”๋ฃจํ‹ด์€ ๋ธ”๋ก๋ผ๋Š” ์ฝ”๋“œ ์กฐ๊ฐ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์ด ๋ธ”๋ก์€ ์‹คํ–‰ ์ค‘์— ์ผ์‹œ ์ค‘๋‹จ๋  ์ˆ˜ ์žˆ๊ณ , ์ดํ›„ ๋‹ค์‹œ ์‹œ์ž‘๋  ์ˆ˜ ์žˆ๋‹ค.

    ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฝ”๋ฃจํ‹ด์€ ๋ธ”๋ก์„ ์‹คํ–‰ํ•˜๋ฉด์„œ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๋‹ค๋ฅธ ์ž‘์—…์œผ๋กœ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. 

     

    fun main() = runBlocking { // this: CoroutineScope
        launch { // launch a new coroutine and continue
            delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
            println("World!") // print after delay
        }
        println("Hello") // main coroutine continues while a previous one is delayed
    }

     

    ๊ฒฐ๊ณผ

    Hello
    World!

     

     

    ์ฝ”๋“œ๋ฅผ ์ชผ๊ฐœ์„œ ์ดํ•ดํ•ด๋ณด์ž!

     

    launch

    launch๋Š” coroutine builder์ด๋‹ค. ๋‚จ์€ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ๋™์‹œ์— ์ƒˆ๋กœ์šด ์ฝ”๋ฃจํ‹ด์„ ์‹œ์ž‘ํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์œ„ ์ฝ”๋“œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋กœ Hello๋ถ€ํ„ฐ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค.

     

    delay

    delay๋Š” ํŠน์ˆ˜ํ•œ ์ผ์‹œ ์ค‘๋‹จ ํ•จ์ˆ˜์ด๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ฝ”๋ฃจํ‹ด์„ ํŠน์ • ์‹œ๊ฐ„๋™์•ˆ ์ผ์‹œ ์ค‘๋‹จ์‹œํ‚จ๋‹ค. ์ผ์‹œ์ค‘๋‹จ์˜ ์˜๋ฏธ๋Š” ํ•ด๋‹น ์ฝ”๋ฃจํ‹ด์ด ์ง€์ •๋œ ์‹œ๊ฐ„๋™์•ˆ ๋Œ€๊ธฐ ์ƒํƒœ๋กœ ๋“ค์–ด๊ฐ„๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋•Œ์˜ ์ผ์‹œ ์ค‘๋‹จ์€ ๊ธฐ๋ณธ ์Šค๋ ˆ๋“œ๋ฅผ ์ฐจ๋‹จํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹  ๋‹ค๋ฅธ ์ฝ”๋ฃจํ‹ด๋“ค์ด ์‹คํ–‰๋˜๋„๋ก ํ—ˆ์šฉํ•˜๊ณ  ๊ธฐ๋ณธ ์Šค๋ ˆ๋“œ๋Š” ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

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

    ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŠน์ • ์‹œ๊ฐ„๋™์•ˆ ์ผ์‹œ ์ค‘๋‹จ๋˜์–ด ๋Œ€๊ธฐํ•˜๋ฉด์„œ๋„ ๋‹ค๋ฅธ ์ฝ”๋ฃจํ‹ด๋“ค์ด ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋‹ค.

     

    runBlocking

    runBlocking์€ ๋˜ํ•œ ์ผ๋ฐ˜์ ์ธ fun main ํ•จ์ˆ˜์ธ  ์ฝ”๋ฃจํ‹ด์ด ์•„๋‹Œ ์„ธ๊ณ„ ์™€ runBlocking { } ๋‚ด๋ถ€์— ์žˆ๋Š” ์ฝ”๋ฃจํ‹ด ์ฝ”๋“œ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋ธŒ๋ฆฟ์ง€๊ฐ™์€ ์ฝ”๋ฃจํ‹ด ๋นŒ๋”์ด๋‹ค. 

     

    ์ฝ”ํ‹€๋ฆฐ์—์„œ ์ฝ”๋ฃจํ‹ด์€ ํ•ญ์ƒ ํŠน์ •ํ•œ corouting scope ๋‚ด์—์„œ ์‹คํ–‰๋˜์–ด์•ผํ•œ๋‹ค. runBlocking์€ ์ฝ”๋ฃจํ‹ด ์Šค์ฝ”ํ”„๋ฅผ ์ œ๊ณตํ•˜๋ฉด์„œ๋„, ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์ธ main๊ณผ ๊ฐ™์€ ์ฝ”๋ฃจํ‹ด์ด ์•„๋‹Œ ํ™˜๊ฒฝ์—์„œ๋„ ์ฝ”๋ฃจํ‹ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

    ๋งŒ์•ฝ runBlocking์„ ์žŠ์–ด๋ฒ„๋ฆฐ๋‹ค๋ฉด IDE์—์„œ๋Š” ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค. 

    ์ด์œ ๋Š” launch์˜ ๊ฒฝ์šฐ CoroutingScope์—์„œ ์ œ๊ณต๋˜๋Š” ํ™•์žฅํ•จ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋”ฐ๋ผ์„œ launch๋Š” runBlocking์ด๋‚˜ ๋‹ค๋ฅธ ์ฝ”๋ฃจํ‹ด ๋นŒ๋”์—†์ด๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค!

     

    ์ •๋ฆฌํ•˜์ž๋ฉด runBlocking์€ ์ฝ”๋ฃจํ‹ด์ด ์–ด๋–ค ์Šค์ฝ”ํ”„์—์„œ ์‹คํ–‰๋˜์–ด์•ผํ•˜๋Š”์ง€ ์ •์˜ํ•˜๊ณ  ๋น„์ฝ”๋ฃจํ‹ดํ™˜๊ฒฝ๊ณผ ์ฝ”๋ฃจํ‹ดํ™˜๊ฒฝ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. 

     

    runBlocking์€ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์Šค๋ ˆ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๊ธฐ๊ฐ„๋™์•ˆ ๋ธ”๋ก๋˜๋ฉฐ, runBlocking {...} ๋ธ”๋ก ๋‚ด์˜ ๋ชจ๋“  ์ฝ”๋ฃจํ‹ด์ด ์‹คํ–‰์„ ์™„๋ฃŒํ•  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๊ฒŒ ํ•œ๋‹ค.

    runBlocking์€ ์•ฑ์˜ ์ตœ์ƒ๋‹จ ์ˆ˜์ค€์—์„œ ์‚ฌ์šฉ๋œ๋‹ค. ์ฃผ๋กœ main์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.

    ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ ์ฝ”๋“œ์—์„œ๋Š” runBlocking์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์ด์ง€ ์•Š๋‹ค. ์ฃผ๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹œ์ž‘์ด๋‚˜ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. ๋ณธ๊ฒฉ์ ์ธ ๋น„๋™๊ธฐ์ฝ”๋“œ ์ž‘์„ฑ์—๋Š” ๋‹ค๋ฅธ ์ฝ”๋ฃจํ‹ด ๋นŒ๋”๋“ค์ด ๋” ์ ์ ˆํ•˜๋‹ค.

     

    Structured concurrency

     

    ์ฝ”๋ฃจํ‹ด์€ ๊ตฌ์กฐ์  ๋™์‹œ์„ฑ์ด๋ผ๋Š” ์›๋ฆฌ๋ฅผ ๋”ฐ๋ฅด๋Š”๋ฐ,

    - ํŠน์ • coroutingscope

    ์ด ์›๋ฆฌ๋Š” ์ƒˆ๋กœ์šด ์ฝ”๋ฃจํ‹ด์€ ํ•ด๋‹น ๋ฒ”์œ„์—์„œ  ์‹œ์ž‘๋œ ์ฝ”๋ฃจํ‹ด์˜ ์ˆ˜๋ช…์„ ์ œํ•œํ•˜๋Š”  CoroutingScope์•ˆ์—์„œ๋งŒ ์‹œ์ž‘๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์›๋ฆฌ์ด๋‹ค.

    - ์ˆ˜๋ช… ์ œํ•œ 

    runBlocking์€ ํŠน์ •ํ•œ ์ฝ”๋ฃจํ‹ด ๋ฒ”์œ„๋ฅผ ์„ค์ •ํ•œ๋‹ค. ์ด๋Š” runBlocking {...} ๋ธ”๋ก ๋‚ด์—์„œ ์‹œ์ž‘๋œ ์ฝ”๋ฃจํ‹ด์ด runBlocking ๋ฒ”์œ„์— ๋ฌถ์—ฌ ์žˆ์–ด์„œ ์ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚  ์ˆ˜ ์—†๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

    - ์™„๋ฃŒ ๋Œ€๊ธฐ

    runBlocking ๋ฒ”์œ„ ๋‚ด์—์„œ ์ฝ”๋ฃจํ‹ด์ด ์‹œ์ž‘๋˜์—ˆ์œผ๋ฏ€๋กœ ์ฝ”๋ฃจํ‹ด์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ Hello ๋‹ค์Œ์— 1์ดˆ์˜ ์ง€์—ฐ ์‹œ๊ฐ„ ํ›„์— World!๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋œ๋‹ค.

    - ๋ˆ„์ถœ ๋ฐฉ์ง€

    ๊ตฌ์กฐ์  ๋™์‹œ์„ฑ์€ ์ฝ”๋ฃจํ‹ด ๋ˆ„์ถœ์„ ๋ฐฉ์ง€ํ•œ๋‹ค. ํŠน์ • ๋ฒ”์œ„ ๋‚ด์—์„œ ์ฝ”๋ฃจํ‹ด์„ ์‹œ์ž‘ํ•˜๋ฉด ํ•ด๋‹น ๋ฒ”์œ„๋Š” ์ž์‹ ์ฝ”๋ฃจํ‹ด์ด ๋ชจ๋‘ ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ์™„๋ฃŒ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋Š” ์ฝ”๋ฃจํ‹ด์ด ์†Œ์‹ค๋˜๊ฑฐ๋‚˜ ๋ฌดํ•œ์  ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•œ๋‹ค.

    - ์—๋Ÿฌ์ฒ˜๋ฆฌ

    ํŠน์ • ๋ฒ”์œ„ ๋‚ด์—์„œ ์‹œ์ž‘๋œ ์ฝ”๋ฃจํ‹ด ๋‚ด์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด ์˜ค๋ฅ˜๊ฐ€  ์ ์ ˆํ•˜๊ฒŒ ๋ณด๊ณ ๋œ๋‹ค.

    ๋˜ํ•œ ๊ตฌ์กฐ์  ์„ฑ๊ฒฉ์€ ์ด๋Ÿฌํ•œ ์˜ค๋ฅ˜๋ฅผ ๋†“์น˜์ง€ ์•Š๊ณ  ์ฒด๊ณ„์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    Extract function refactoring

     

    ์ฒ˜์Œ ์ฝ”๋“œ 

    fun main() = runBlocking { // this: CoroutineScope
        launch { // launch a new coroutine and continue
            delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
            println("World!") // print after delay
        }
        println("Hello") // main coroutine continues while a previous one is delayed
    }

     

    ๋ฆฌํŒฉํ† ๋งํ•œ ์ฝ”๋“œ

    fun main() = runBlocking { // this: CoroutineScope
        launch { doWorld() }
        println("Hello")
    }
    
    // this is your first suspending function
    suspend fun doWorld() {
        delay(1000L)
        println("World!")
    }

     

    ๊ฒฐ๊ณผ

    Hello
    World!

     

    suspend modifier์„ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์–ป๊ฒŒ ๋œ๋‹ค. ์ด ํ•จ์ˆ˜๊ฐ€ ์šฐ๋ฆฌ์˜ ์ฒซ๋ฒˆ์งธ suspending function!

    suspending function์€ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์™€ ๊ฐ™์ด ์ฝ”๋ฃจํ‹ด ๋‚ด์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

    ๊ทธ๋Ÿฌ๋‚˜ suspending function์˜ ์ถ”๊ฐ€์ ์ธ ํŠน์ง•์€ ์ฝ”๋ฃจํ‹ด์˜ ์‹คํ–‰์„ ์ผ์‹œ์ค‘๋‹จ ์‹œํ‚ค๋Š” delay์™€ ๊ฐ™์€ suspending function์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค!

     

    scope builder

    ๋‹ค์–‘ํ•œ ๋นŒ๋”์— ์˜ํ•ด ์ œ๊ณต๋˜๋Š” ์ฝ”๋ฃจํ‹ด scope ์ด์™ธ์—๋„, coroutineScope builder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„ˆ๋งŒ์˜ ์ž์ฒด scope๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์€ ์ฝ”๋ฃจํ‹ด ์Šค์ฝ”ํ”„๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ชจ๋“  ์ž์‹์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ์™„๋ฃŒ๋˜์ง€ ์•Š๋Š”๋‹ค.

     

    runBlocking๊ณผ coroutingScope Builder๋“ค์€ ๋‘˜๋‹ค ๊ทธ๋“ค์˜ body์™€  ๋ชจ๋“  ์ž์‹์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋น„์Šทํ•ด๋ณด์ผ ์ˆ˜ ์žˆ๋‹ค! ๊ทธ๋Ÿฌ๋‚˜ ์ฃผ์š” ์ฐจ์ด๊ฐ€ ์กด์žฌํ•œ๋‹ค. 

     

    runBlocking์€ ํ˜„์žฌ์˜ ์Šค๋ ˆ๋“œ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ ์œ„ํ•ด  ๋ง‰๋Š”๋‹ค. ๋ฐ˜๋ฉด coroutingScope๋Š” ๊ทธ์ € ์ผ์‹œ์ค‘๋‹จ๋˜์–ด์„œ ๊ธฐ๋ณธ ์Šค๋ ˆ๋“œ(underlying thread)๋ฅผ ๋‹ค๋ฅธ ์šฉ๋„๋กœ ํ•ด์ œํ•œ๋‹ค(release ํ•œ๋‹ค)

    ์ด๋Ÿฌํ•œ ์ฐจ์ด๋•Œ๋ฌธ์—, runBlocking์€ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์ด๊ณ  coroutingScope๋Š” ์ผ์‹œ์ค‘๋‹จ ํ•จ์ˆ˜(suspending function)์ด๋‹ค.

     

    ์šฐ๋ฆฌ๋Š” ์–ด๋– ํ•œ suspending function์—์„œ๋„ coroutineScope๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด Hello์™€ World๋ฅผ ๋™์‹œ์— ์ถœ๋ ฅํ•˜๋Š” ๋ถ€๋ถ„์„ suspend fun doWorld( ) ํ•จ์ˆ˜๋กœ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋‹ค.

     

    Scope builder and concurrency

    coroutineScope builder๋Š” (์—ฌ๋Ÿฌ ๋™์‹œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด) ์–ด๋–ค ์ผ์‹œ์ค‘๋‹จ ํ•จ์ˆ˜ ๋‚ด์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    // Sequentially executes doWorld followed by "Done"
    fun main() = runBlocking {
        doWorld()
        println("Done")
    }
    
    // Concurrently executes both sections
    suspend fun doWorld() = coroutineScope { // this: CoroutineScope
        launch {
            delay(2000L)
            println("World 2")
        }
        launch {
            delay(1000L)
            println("World 1")
        }
        println("Hello")
    }

     

    ๊ฒฐ๊ณผ

    Hello
    World 1
    World 2
    Done

     

    ์—ฌ๊ธฐ์„œ launch {...} ๋‚ด๋ถ€์˜ ๋‘ ์ฝ”๋“œ ์กฐ๊ฐ๋ธ”๋ก์€ ๋™์‹œ์— ์‹คํ–‰๋œ๋‹ค.  ๋จผ์ € World1์ด ์ถœ๋ ฅ๋˜๊ณ , ์‹œ์ž‘์œผ๋กœ๋ถ€ํ„ฐ 2์ดˆ ํ›„์—  World2๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

     

    doWorld ๋‚ด๋ถ€์˜ coroutingScope๋Š” ์˜ค์ง ๋‘˜ ๋‹ค ์™„๋ฃŒ๋œ ์ดํ›„์—๋งŒ ์™„๋ฃŒ๋œ๋‹ค. ๋”ฐ๋ผ์„œ doWorldํ•จ์ˆ˜๋Š” Done ๋ฌธ์ž์—ด์ด ์™„๋ฃŒ๋œ ์ดํ›„์— ๋ฐ˜ํ™˜๋˜๊ณ  ์ถœ๋ ฅ๋˜๋„๋ก ํ•œ๋‹ค.

     

    An explicit job

    launch๋Š” ์‹œ์ž‘๋œ coroutine์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ช…์‹œ์ ์œผ๋กœ coroutine๋“ค์˜ ์™„๋ฃŒ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š”  jop ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” corouting builder์ด๋‹ค .

    fun main() = runBlocking {
        // Using launch to start a coroutine and get a reference to its Job
        val job = launch {
            // Simulating some asynchronous task with delay
            delay(1000L)
            println("World!")
        }
    
        // Additional code in the main coroutine while the child coroutine is running
    
        println("Hello")
    
        // Explicitly waiting for the completion of the child coroutine using job.join()
        job.join()
    
        // Code after the child coroutine is completed
        println("Done")
    }

     

    * launch: ์ƒˆ๋กœ์šด ์ฝ”๋ฃจํ‹ด์„ ์‹œ์ž‘ํ•˜๊ณ  ๊ทธ๋“ค์˜ job ์ฐธ์กฐ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด launch๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

     * job :lauch๋กœ ์‹œ์ž‘๋œ ์ฝ”๋ฃจํ‹ด์€ val job = launch { ...}์„ ํ†ตํ•ด Job ๊ฐ์ฒด์ธ job์„ ์–ป๋Š”๋‹ค. 

    * job.join : job์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋ผ๊ณ  ํ˜„์žฌ์˜ ์ฝ”๋ฃจํ‹ด์—๊ฒŒ ๋ช…๋ นํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ ์ฝ”๋ฃจํ‹ด์ธ main ์ฝ”๋ฃจํ‹ด์€ ์ž์‹ ์ฝ”๋ฃจํ‹ด์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ์ฐจ๋‹จ๋˜๊ณ  ๊ทธ ํ›„์— Done ๋ฌธ์ž์—ด์ด ๋ฐ˜ํ™˜๋œ๋‹ค.

     

     

    ๊ฒฐ๊ณผ

    Hello
    World!
    Done

     

     

    Coroutines are light-weight

    ์ฝ”๋ฃจํ‹ด๋“ค์€ JVM ์Šค๋ ˆ๋“œ๋ณด๋‹ค  ๋” ์ ์€ ์ž์›์„ ์‚ฌ์šฉํ•œ๋‹ค. 

    728x90