当前位置:网站首页>Kotlin composite suspend function

Kotlin composite suspend function

2022-06-24 13:39:00 day_ moon

//1. Execute in order 
fun main1() = runBlocking<Unit> {
    val time = measureTimeMillis { // computing time 
        var one = doOne()
        var two = doTwo()
        println("onetwo Time is ${one+two}")// Add up 
    }
    println("Time is ${time}")
}
suspend fun doOne(): Int {
    delay(1000)// Delay 1 second 
    return 1
}
suspend fun doTwo(): Int {
    delay(2000)// Delay 2 second 
    return 2
}
//2. Concurrent execution  async
fun mainAsync() = runBlocking<Unit> {
    val time = measureTimeMillis { // computing time 
        var one = async { doOne()}// async  Start a separate collaboration   Returns a lightweight nonblocking  Deferred  object 
        var two =  async { doTwo()}//
        println("onetwo Time is ${one.await()+two.await()}")//one.await() Get the returned value 
    }
    println("Time is ${time}")
}
//3. Inert start 
fun mainLazy() = runBlocking<Unit> {
    val time = measureTimeMillis { // computing time 
        var one = async(start = CoroutineStart.LAZY) { doOne()}// async  Start a separate collaboration   Returns a lightweight nonblocking  Deferred  object 
        var two =  async (start = CoroutineStart.LAZY){ doTwo()}
        one.start()// Start execution of collaboration  , Note that if   First call await()  Will start the process and wait for it to complete 
        two.start()
        println("onetwo Time is ${one.await()+two.await()}")//one.await() Get the returned value 
    }
    println("Time is ${time}")
}
//4. Concurrent structure 
fun main_async1() = runBlocking<Unit> {
    val time = measureTimeMillis { // computing time 
        doThings()
    }
    println("Time is ${time}")
}

 suspend fun doThings():Int=coroutineScope  {
    var one = async() { doOnes() }// async  Start a separate collaboration   Returns a lightweight nonblocking  Deferred  object 
    var two = async() { doTwos() }
     println("onetwo Time is ${one.await()+two.await()}")//one.await() Get the returned value 
    one.await() + two.await()//one.await() Get the returned value 
}
suspend fun doOnes(): Int {
    delay(1000) // pretend we are doing something useful here
    return 1
}
suspend fun doTwos(): Int {
    delay(2000) // pretend we are doing something useful here
    return 2
}
fun main_async2() = runBlocking<Unit> {
    try {
        currentSum()
    }catch (e:Exception){
        println("main_async2 Exception..")
    }

}

 suspend fun currentSum():Int= coroutineScope {
     val one=async<Int> {
         try {
             delay(1000)
         }finally {
             println("one finally")
         }
         1
     }
     val two=async<Int> {
         delay(2000)
         println("two exception..")
         throw ArithmeticException()// Pre subclass co process throwing exception   Then the parent class coroutine throws an exception 
     }
     one.await()+two.await()
}
原网站

版权声明
本文为[day_ moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241048331384.html