funmain() = runBlocking<Unit> { //sampleStart val time = measureTimeMillis { val one = doSomethingUsefulOne() val two = doSomethingUsefulTwo() println("The answer is ${one + two}") } println("Completed in $time ms") //sampleEnd }
suspendfundoSomethingUsefulOne(): Int { delay(1000L) // pretend we are doing something useful here return13 }
suspendfundoSomethingUsefulTwo(): Int { delay(1000L) // pretend we are doing something useful here, too return29 }
funmain() = runBlocking<Unit> { //sampleStart val time = measureTimeMillis { val one = async { doSomethingUsefulOne() } val two = async { doSomethingUsefulTwo() } println("The answer is ${one.await() + two.await()}") } println("Completed in $time ms") //sampleEnd }
suspendfundoSomethingUsefulOne(): Int { delay(1000L) // pretend we are doing something useful here return13 }
suspendfundoSomethingUsefulTwo(): Int { delay(1000L) // pretend we are doing something useful here, too return29 }
funmain() = runBlocking<Unit> { //sampleStart val time = measureTimeMillis { val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() } val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() } // some computation one.start() // start the first one two.start() // start the second one println("The answer is ${one.await() + two.await()}") } println("Completed in $time ms") //sampleEnd }
suspendfundoSomethingUsefulOne(): Int { delay(1000L) // pretend we are doing something useful here return13 }
suspendfundoSomethingUsefulTwo(): Int { delay(1000L) // pretend we are doing something useful here, too return29 }
//sampleStart // note that we don't have `runBlocking` to the right of `main` in this example funmain() { val time = measureTimeMillis { // we can initiate async actions outside of a coroutine val one = somethingUsefulOneAsync() val two = somethingUsefulTwoAsync() // but waiting for a result must involve either suspending or blocking. // here we use `runBlocking { ... }` to block the main thread while waiting for the result runBlocking { println("The answer is ${one.await() + two.await()}") } } println("Completed in $time ms") } //sampleEnd
suspendfunconcurrentSum(): Int = coroutineScope { val one = async { doSomethingUsefulOne() } val two = async { doSomethingUsefulTwo() } one.await() + two.await() }
funmain() = runBlocking<Unit> { //sampleStart val time = measureTimeMillis { println("The answer is ${concurrentSum()}") } println("Completed in $time ms") //sampleEnd }
suspendfunconcurrentSum(): Int = coroutineScope { val one = async { doSomethingUsefulOne() } val two = async { doSomethingUsefulTwo() } one.await() + two.await() }
suspendfundoSomethingUsefulOne(): Int { delay(1000L) // pretend we are doing something useful here return13 }
suspendfundoSomethingUsefulTwo(): Int { delay(1000L) // pretend we are doing something useful here, too return29 }
suspendfunfailedConcurrentSum(): Int = coroutineScope { val one = async<Int> { try { delay(Long.MAX_VALUE) // Emulates very long computation 42 } finally { println("First child was cancelled") } } val two = async<Int> { println("Second child throws an exception") throw ArithmeticException() } one.await() + two.await() }
需要注意协程 one 和正在等待的父级是如何在协程 two 失败时取消的
1 2 3
Second child throws an exception First child was cancelled Computation failed with ArithmeticException