Kotlin Async

package com.tiger.mykotlinapp.scope

import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking

fun main():Unit = runBlocking {

    val d = async(start = CoroutineStart.LAZY){
        test()
    }
    println("hello")
    println("hello")

    println("hello")
    println("hello")
    println("hello")
    println("hello")
    println("hello")
    println(d.await())//代表开启线程执行, 拿到返回值


}


suspend fun test():Int{
    println("执行中")
    delay(1000)
    return 100
}
package com.tiger.mykotlinapp.scope

import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.selects.select

fun main():Unit = runBlocking {

    val d = async(start = CoroutineStart.LAZY){

        test1("1")
    }

    val c = async(start = CoroutineStart.LAZY){
        delay(2000)
        test1("2")
    }

    println(select<String> {
        //阻塞主线程把协程等待结束再继续执行 谁先执行完毕,就退出返回先结束的那个返回值

        c.onAwait.invoke {
            it
        }
        d.onAwait.invoke {
            it
        }


    })
    println("end")
//    println(d.await())//代表开启线程执行, 拿到返回值


}


suspend fun test1(a:String):String{
    println("执行中$a")
    delay(1000)
    return a
}
package com.tiger.mykotlinapp.scope

import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.selects.select

fun main():Unit = runBlocking {

    val d = async(start = CoroutineStart.LAZY){

        test3("1")
    }

    val c = async(start = CoroutineStart.LAZY){
        delay(2000)
        test3("2")
    }

    println(select<String> {
        //阻塞主线程把协程等待结束再继续执行 谁先执行完毕,就退出返回先结束的那个返回值

        c.onAwait.invoke {
            it
        }
        d.onAwait.invoke {
            it
        }


    })
    println("end")
//    println(d.await())//代表开启线程执行, 拿到返回值


}


suspend fun test3(a:String):String{
    println("执行中$a")
    delay(1000)
    return a
}

getCompleted

这个是立刻获取协程异步执行的值

如果还没执行完毕就抛异常

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-17 13:44:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-17 13:44:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-17 13:44:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-17 13:44:01       20 阅读

热门阅读

  1. Python 发微信:实现自动化沟通的利器

    2024-01-17 13:44:01       33 阅读
  2. sqlserver2012 跨服务器查询

    2024-01-17 13:44:01       40 阅读
  3. ARCGIS PRO SDK 地图图层单一符号化_____面图层

    2024-01-17 13:44:01       35 阅读
  4. Flutter开发 键盘弹起导致底部溢出问题

    2024-01-17 13:44:01       34 阅读
  5. C#学习教程

    2024-01-17 13:44:01       37 阅读
  6. 黑洞数(C语言)

    2024-01-17 13:44:01       30 阅读
  7. 快速了解STM32的ADC功能,从入门到精通

    2024-01-17 13:44:01       31 阅读
  8. Github Copilot 的使用方法和快捷键*

    2024-01-17 13:44:01       45 阅读
  9. Nue.js 是什么?

    2024-01-17 13:44:01       28 阅读
  10. What is `HttpServletRequestWrapper` does?

    2024-01-17 13:44:01       35 阅读
  11. ConcurrentHashMap源码解析

    2024-01-17 13:44:01       43 阅读