=================
== The Archive ==
=================

[Kotlin Coroutines] 9장. 취소

|

개요

기본적인 취소

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
suspend fun main(): Unit =
    coroutineScope {
        val job =
            launch {
                repeat(1_000) { i ->
                    delay(200)
                    println("Printing $i")
                }
            }

        delay(1100)
        job.cancel()
        job.join()
        println("Cancelled successfully")
    }

취소는 어떻게 작동하는가?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
suspend fun main(): Unit =
    coroutineScope {
        val job = Job()
        launch(job) {
            try {
                repeat(1_000) { i ->
                    delay(200)
                    println("Printing $i")
                }
            } catch (e: CancellationException) {
                println(e)
                throw e
            }
        }
        delay(1100)
        job.cancelAndJoin()
        println("Cancelled successfully")
        delay(1000)
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
suspend fun main(): Unit =
    coroutineScope {
        val job = Job()
        launch(job) {
            try {
                delay(Random.nextLong(2000))
                println("Done")
            } finally {
                print("Will always be printed")
            }
        }
        delay(1000)
        job.cancelAndJoin()
    }

취소 중 코루틴을 한 번 더 호출하기

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
suspend fun main(): Unit =
    coroutineScope {
        val job = Job()
        launch(job) {
            try {
                delay(2000)
                println("Job is done")
            } finally {
                println("Finally")
                launch { // will be ignored
                    println("Will not be printed")
                }
                delay(1000) // here exception is thrown
                println("Will not be printed")
            }
        }
        delay(1000)
        job.cancelAndJoin()
        println("Cancel done")
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
suspend fun main(): Unit =
    coroutineScope {
        val job = Job()
        launch(job) {
            try {
                delay(200)
                println("Coroutine finished")
            } finally {
                println("Finally")
                withContext(NonCancellable) {
                    delay(1000L)
                    println("Cleanup done")
                }
            }
        }
        delay(100)
        job.cancelAndJoin()
        println("Done")
    }

invokeOnCompletion

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
suspend fun main(): Unit =
    coroutineScope {
        val job =
            launch {
                delay(1000)
            }
        job.invokeOnCompletion { exception: Throwable? ->
            println("Finished")
        }
        delay(400)
        job.cancelAndJoin()
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
suspend fun main(): Unit =
    coroutineScope {
        val job =
            launch {
                delay(Random.nextLong(2400))
                println("Finished")
            }
        delay(800)
        job.invokeOnCompletion { exception: Throwable? ->
            println("Will always be printed")
            println("The exception was: $exception")
        }
        delay(800)
        job.cancelAndJoin()
    }

중단될 수 없는 걸 중단하기

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
suspend fun main(): Unit =
    coroutineScope {
        val job = Job()
        launch(job) {
            repeat(1_000) { i ->
                Thread.sleep(200) // We might have some
                // complex operations or reading files here
                println("Printing $i")
            }
        }
        delay(1000)
        job.cancelAndJoin()
        println("Cancelled successfully")
        delay(1000)
    }
// 계속 출력됨...

suspendCancellableCoroutine

요약