Notice
Recent Posts
Recent Comments
Link
ยซ   2025/04   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฑ dreaming DiNO

[Android] Coroutine ์ฝ”๋ฃจํ‹ด ์˜ˆ์ œ1 ๋ณธ๋ฌธ

Android

[Android] Coroutine ์ฝ”๋ฃจํ‹ด ์˜ˆ์ œ1

MK_____ 2023. 7. 7. 14:01

Basic

SecondFragment.kt

binding.checkBtn.setOnClickListener {
    secondViewModel.exampleSuspend()
}

 

SecondViewModel.kt

fun exampleSuspend() = runBlocking {
    
    launch {
        repeat(5) { i ->
            println("Coroutine A, $i")
        }
    }
    
    launch {
        repeat(5) { i ->
            println("Coroutine B, $i")
        }
    }

    println("Coroutine Outer")
}
I: Coroutine Outer
I: Coroutine A, 0
I: Coroutine A, 1
I: Coroutine A, 2
I: Coroutine A, 3
I: Coroutine A, 4
I: Coroutine B, 0
I: Coroutine B, 1
I: Coroutine B, 2
I: Coroutine B, 3
I: Coroutine B, 4

launch ๊ฐ€ ๋จผ์ € ์‹คํ–‰๋˜์„œ Coroutine A ๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ์ฐํž ๊ฒƒ ๊ฐ™์•˜์ง€๋งŒ,

launch๋Š” ์ฝ”๋ฃจํ‹ด์ด ์‹คํ–‰๋˜๊ธฐ ์œ„ํ•ด ์Šค์ผ€์ฅด๋ง์ด ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

→ Coroutine Outer ๋จผ์ € ์ฐํžˆ๊ณ  launch ๋กœ ์ƒ์„ฑ๋œ ์ฝ”๋ฃจํ‹ด์ด ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰๋œ๋‹ค. 

 


Cancellation and Timeouts

→ ์ฝ”๋ฃจํ‹ด์„ ์ทจ์†Œ ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, ์ฝ”๋ฃจํ‹ด์ด ํ˜‘์กฐ์ ์ด์–ด์•ผ ํ•จ

๋ฐฉ๋ฒ• 1. suspend ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” job.cancleAndJoin()

๋ฐฉ๋ฒ• 2. ์ผ๋ฐ˜ ํ•จ์ˆ˜ ์ผ๋•Œ๋Š” ์ค‘๋‹จ์ด ์•ˆ๋จ → isActive ์ƒํƒœ ์ฒดํฌํ•˜์—ฌ ์ฝ”๋ฃจํ‹ด ์ทจ์†Œํ•˜๊ธฐ

fun exampleSuspend() = runBlocking {

        val job = launch {
            repeat(10) { i ->
                println("job: I'm sleeping $i...")
                delay(1000L)
            }
        }

        delay (1500L)
        println("main: I'm tired of waiting!")
//        job.cancel() // ์ฝ”๋ฃจํ‹ด ์ข…๋ฃŒ
//        job.join() // job ์ˆ˜ํ–‰์ด ๋๋‚˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฐ๋‹ค
        job.cancelAndJoin()
        println("main: Now I can quit")
    }
I: job: I'm sleeping 0...
I: job: I'm sleeping 1...
I: main: I'm tired of waiting!
I: main: Now I can quit

 

๋ฆฌ์†Œ์Šค ํ•ด์ œ ์œ„์น˜

fun exampleSuspend() = runBlocking {

        val job = launch {
            try {
                repeat(100) { i ->
                    println("job: I'm sleeping $i...")
                    delay(1000L) // suspend ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ด ์ฝ”๋ฃจํ‹ด์€ ์ทจ์†Œํ•˜๊ธฐ์— ํ˜‘์กฐ์ ์ธ ํ˜•ํƒœ
                }
            } finally {
                // ์ฝ”๋ฃจํ‹ด์„ ์ทจ์†Œํ•˜๋ฉด, suspend ํ•จ์ˆ˜๊ฐ€ ์žฌ๊ฐœ๋˜๋ฉด์„œ exception ์„ ๋˜์ง
                // ์ด๊ณณ์—์„œ ๋ฆฌ์†Œ์Šค ํ•ด์ œ
                println("job: I'm running finally")
            }
        }

        delay (1500L)
        println("main: I'm tired of waiting!")
//        job.cancel() // ์ฝ”๋ฃจํ‹ด ์ข…๋ฃŒ
//        job.join() // job ์ˆ˜ํ–‰์ด ๋๋‚˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฐ๋‹ค
        job.cancelAndJoin()
    }
I: job: I'm sleeping 0...
I: job: I'm sleeping 1...
I: main: I'm tired of waiting!
I: job: I'm running finally

 

Timeouts

fun exampleSuspend() = runBlocking {
    val result = withTimeoutOrNull(1300L) {
        repeat(10) { i ->
            println("I'm sleeping $i ...")
            delay(500L)
        }
        "Done"
    }
    println("Result is $result")
}
I: I'm sleeping 0 ...
I: I'm sleeping 1 ...
I: I'm sleeping 2 ...
I: Result is null