美文网首页
Android Kotlin协程

Android Kotlin协程

作者: Ello_Orld | 来源:发表于2021-02-19 21:52 被阅读0次
    1. launch函数
    private fun testLaunch() {
            val time = measureTimeMillis {
                GlobalScope.launch {
                    delay(1000)
                    println("${Thread.currentThread()} --AAAAAA")
                }
                GlobalScope.launch {
                    delay(1000)
                    println("${Thread.currentThread()} --BBBBB")
                }
                println("${Thread.currentThread()}----CCCCCCCC")
                Thread.sleep(2200)
            }
            println("总耗时 $time")
        }
    

    结果:

    Thread[main,5,main]----CCCCCCCC
    Thread[DefaultDispatcher-worker-2 @coroutine#2,5,main] --BBBBB
    Thread[DefaultDispatcher-worker-1 @coroutine#1,5,main] --AAAAAA
    总耗时 2258
    
    1. async函数
      和launch类似,但是async返回值是defferd,可以阻塞获取结果,也可以取消。
    private fun testLaunch() {
            val time = measureTimeMillis {
                GlobalScope.launch {
                    delay(1000)
                    println("${Thread.currentThread()} --AAAAAA")
                }
                GlobalScope.launch {
                    delay(1000)
                    println("${Thread.currentThread()} --BBBBB")
                }
                println("${Thread.currentThread()}----CCCCCCCC")
                Thread.sleep(2200)
            }
            println("总耗时 $time")
        }
    
    
    1. runBlock 阻塞线程,一般可以用于桥接普通函数和协程(普通函数想要调用协程函数)
    private fun testRunBlocking() {
            val time = measureTimeMillis {
                runBlocking {
                    println("before block ${Thread.currentThread()}")
                    delay(500)
                    println("after block ${Thread.currentThread()}")
                }
                println("testRunBlock block ${Thread.currentThread()}")
            }
            println("总耗时:$time")
        }
    

    输出:

    before block Thread[main @coroutine#1,5,main]
    after block Thread[main @coroutine#1,5,main]
    testRunBlock block Thread[main,5,main]
    总耗时:563
    
    1. join
    {
      val a2 :Deferred<Unit> = async {
                    repeat(20) {
                        println("${Thread.currentThread()}  --  A2")
                        delay(200)
                    }
                }
    
       a2.join()   //必须 a2执行 结束, 才会往下执行
       println("外部")
    }
    

    a2全部执行完成后,才会继续往下执行

    1. cancel
      val a2 :Deferred<Unit> = async {
                    repeat(20) {
                        println("${Thread.currentThread()}  --  A2")
                        delay(200)
                    }
                }
    
                val a1:Deferred<Unit> = async {
                    println("-- A1  --")
    
                    a2.cancel("停止了")
                }
    //            a2.join()   //必须 a2执行 结束, 才会往下执行
    //            a2.await()
                println("外部")
    

    结果:执行A1的时候,a2任务被取消,不再执行

    1. timeOut
    private fun testTimeOut() {
            runBlocking {
                withTimeout(3000) {
                    launch {
                        repeat(20) {
                            println("AAAAAAA")
                            delay(500)
                        }
                    }
                }
            }
        }
    

    实际执行:


    image.png

    超时自动抛出异常。
    如果不想要抛出异常,可以使用withTimeOutOrNull函数,超时时返回结果为null,而不抛出异常:

    private fun testTimeOut() {
            runBlocking {
                withTimeoutOrNull(3000) {
                    launch {
                        repeat(20) {
                            println("AAAAAAA")
                            delay(500)
                        }
                    }
                }
            }
        }
    

    打印结果为:

    AAAAAAA
    AAAAAAA
    AAAAAAA
    AAAAAAA
    AAAAAAA
    AAAAAAA
    
    Process finished with exit code 0
    
    1. await
    private fun testAwait() {
            runBlocking {
                val time = measureTimeMillis {
    
                    val a2: Deferred<Int> = async {
                        println("A2")
                        delay(3000)
                        100
                    }
    
                    val a1: Deferred<Int> = async {
                        println("A1")
                        delay(2000)
                        99
                    }
    
                    println("结果 = ${a1.await()} -- ${a2.await()}")
                }
                println("函数总耗时 --- $time" )
    
            }
        }
    

    结果:同时取await,耗时是用时最长的那个

    A2
    A1
    结果 = 99 -- 100
    函数总耗时 --- 3014
    
    Process finished with exit code 0
    

    相关文章

      网友评论

          本文标题:Android Kotlin协程

          本文链接:https://www.haomeiwen.com/subject/xdczxltx.html