기본적인 순차처리

fun main()  = runBlocking {
    val times = measureTimeMillis {
        val one = doSomethingUsefulOne()
        val two = doSomethingUsefulTwo()
        println("The answer is ${one + two}")
    }
    println("Completed in $times ms")
}

suspend fun doSomethingUsefulOne(): Int {
    delay(1000)
    return 13
}

suspend fun doSomethingUsefulTwo(): Int {
    delay(1000)
    return 29
}

// The answer is 42
// Completed in 2017 ms

async를 사용한 동시성

fun main()  = runBlocking {
    val times = measureTimeMillis {
        val one = async { doSomethingUsefulOne() }
        val two = async { doSomethingUsefulTwo() }
        println("The answer is ${one.await() + two.await()}")
    }
    println("Completed in $times ms")
}

// The answer is 42
// Completed in 1015 ms

async lazy하게 시작하기

fun main()  = runBlocking {
    val time = measureTimeMillis {
        val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
        val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
        one.start() 
        two.start() 
        println("The answer is ${one.await() + two.await()}")
    }
    println("Completed in $time ms")
}

// The answer is 42
// Completed in 1015 ms

비동기 스타일 함수

@OptIn(DelicateCoroutinesApi::class)
fun somethingUsefulOneAsync() = GlobalScope.async {
    doSomethingUsefulOne()
}

구조화된 동시성과 async

suspend fun concurrentSum(): Int = coroutineScope {
    val one = async { doSomethingUsefulOne() }
    val two = async { doSomethingUsefulTwo() }
    one.await() + two.await()
}

fun main() = runBlocking {
    val times = measureTimeMillis {
        println("The answer is ${concurrentSum()}")
    }
    println("Completed in $times ms")
}

// The answer is 42
// Completed in 1018 ms

위 코드처럼 coroutineScope 로 함수를 작성하면 concurrentSum 함수 내부에서 예외가 발생 되었을 때, 스코프 내부에서 실행된 모든 코루틴들이 취소됨.

취소는 언제나 코루틴의 계층 구조를 통해 전파된다