码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • kotlin协程并发/并行与串行互相切换,CoroutineScope与await


    kotlin协程并发/并行与串行互相切换,CoroutineScope与await

     

    1. import kotlinx.coroutines.CoroutineScope
    2. import kotlinx.coroutines.Dispatchers
    3. import kotlinx.coroutines.delay
    4. import kotlinx.coroutines.launch
    5. import java.time.LocalTime
    6. fun main(args: Array<String>) {
    7. println("${LocalTime.now()} - main start")
    8. CoroutineScope(Dispatchers.Default).launch {
    9. // 并发执行
    10. this.launch {
    11. println("${LocalTime.now()} A start")
    12. delay(1000)
    13. println("${LocalTime.now()} A end")
    14. }
    15. this.launch {
    16. println("${LocalTime.now()} B start")
    17. delay(1500)
    18. println("${LocalTime.now()} B end")
    19. }
    20. }
    21. println("${LocalTime.now()} - main end")
    22. }

    87f9d76084ad4426bafefd397bbdf68a.png

     输出表明main退出后,A,B均没有输出。CoroutineScope(Dispatchers.IO).launch新起的任务不阻塞主main任务执行流程。

    如果换成runBlocking(Dispatchers.IO):

    1. import kotlinx.coroutines.*
    2. import java.time.LocalTime
    3. fun main(args: Array<String>) {
    4. println("${LocalTime.now()} - main start")
    5. runBlocking(Dispatchers.IO) {
    6. // 并发执行
    7. this.launch {
    8. println("${LocalTime.now()} A start")
    9. delay(1000)
    10. println("${LocalTime.now()} A end")
    11. }
    12. this.launch {
    13. println("${LocalTime.now()} B start")
    14. delay(1500)
    15. println("${LocalTime.now()} B end")
    16. }
    17. }
    18. println("${LocalTime.now()} - main end")
    19. }

    1f6b5f0d021045d7893676b304f93a40.png

     

     

    看一下async

    1. import kotlinx.coroutines.*
    2. import java.time.LocalTime
    3. fun main(args: Array<String>) {
    4. println("${LocalTime.now()} - main start")
    5. CoroutineScope(Dispatchers.IO).launch() {
    6. // 并发执行
    7. this.async {
    8. println("${LocalTime.now()} A start")
    9. delay(1000)
    10. println("${LocalTime.now()} A end")
    11. }
    12. this.async {
    13. println("${LocalTime.now()} B start")
    14. delay(1500)
    15. println("${LocalTime.now()} B end")
    16. }
    17. }
    18. println("${LocalTime.now()} - main end")
    19. }

    b2bbb1a5826f4435b74dd643b3b9eabc.png

     如果main线程休息1000ms:

    1. import kotlinx.coroutines.*
    2. import java.time.LocalTime
    3. fun main(args: Array<String>) {
    4. println("${LocalTime.now()} - main start")
    5. CoroutineScope(Dispatchers.IO).launch() {
    6. // 并发执行
    7. this.async {
    8. println("${LocalTime.now()} A start")
    9. delay(1000)
    10. println("${LocalTime.now()} A end")
    11. }
    12. this.async {
    13. println("${LocalTime.now()} B start")
    14. delay(1500)
    15. println("${LocalTime.now()} B end")
    16. }
    17. }
    18. Thread.sleep(1000)
    19. println("${LocalTime.now()} - main end")
    20. }

    bc1857a373b2488da03f53447e70202b.png

     

     

    1. import kotlinx.coroutines.*
    2. import java.time.LocalTime
    3. fun main(args: Array<String>) {
    4. println("${LocalTime.now()} - main start")
    5. runBlocking {
    6. CoroutineScope(Dispatchers.IO).launch() {
    7. val task1 = this.async {
    8. println("${LocalTime.now()} A start")
    9. delay(1000)
    10. println("${LocalTime.now()} A end")
    11. "task1 return"
    12. }
    13. val task2 = this.async {
    14. println("${LocalTime.now()} B start")
    15. delay(1500)
    16. println("${LocalTime.now()} B end")
    17. "task2 return"
    18. }
    19. val t1 = task1.await()
    20. println("${LocalTime.now()} $t1")
    21. val t2 = task2.await()
    22. println("${LocalTime.now()} $t2")
    23. }
    24. }
    25. println("${LocalTime.now()} - main end")
    26. }

    39e1e4a2e3e3478da0ef690db39afe19.png

     

    总体上CoroutineScope(Dispatchers.IO).launch()的意义在于新起一个线程,不阻塞主main线程,由于

     

     

     

    理解await

    1. import kotlinx.coroutines.*
    2. import java.time.LocalTime
    3. fun main(args: Array<String>) {
    4. println("${LocalTime.now()} - main start")
    5. runBlocking {
    6. //launch() {
    7. val task1 = this.async {
    8. println("${LocalTime.now()} A start")
    9. delay(3000)
    10. println("${LocalTime.now()} A end")
    11. "task1 return"
    12. }
    13. val task2 = this.async {
    14. println("${LocalTime.now()} B start")
    15. delay(1000)
    16. println("${LocalTime.now()} B end")
    17. "task2 return"
    18. }
    19. val t1 = task1.await()
    20. println("${LocalTime.now()} $t1")
    21. val t2 = task2.await()
    22. println("${LocalTime.now()} $t2")
    23. //}
    24. }
    25. println("${LocalTime.now()} - main end")
    26. }

    1ec6b998a72b411fab336c7ce142b909.png

     虽然task2很快就完成,因为await,所以必须等待task1返回。

     

     

     

    kotlin协程async与await_zhangphil的博客-CSDN博客runBlocking 内部启动的3个协程做耗时操作,从输出可以看到3个协程交叉并发执行,runBlocking 会等到3个协程执行结束后才退出,输出结果有明确先后顺序。一般编程的技法,比如,在Android中,假设在主线程中实现了一个函数,但该函数是耗时操作,毫无疑问,需要将这个函数的实现切入非主线程中操作,那么可以设计一种托管的函数,在托管的函数里面干脏活,处理完成后,把结果抛到主线程。结果1-a: 5 - tid:22。结果1-b: 5 - tid:24。结果2-a: 9 - tid:22。https://blog.csdn.net/zhangphil/article/details/129268399

    https://zhangphil.blog.csdn.net/article/details/129265638https://zhangphil.blog.csdn.net/article/details/129265638

    kotlin协程、线程切换,函数方法委托_zhangphil的博客-CSDN博客runBlocking 内部启动的3个协程做耗时操作,从输出可以看到3个协程交叉并发执行,runBlocking 会等到3个协程执行结束后才退出,输出结果有明确先后顺序。一般编程的技法,比如,在Android中,假设在主线程中实现了一个函数,但该函数是耗时操作,毫无疑问,需要将这个函数的实现切入非主线程中操作,那么可以设计一种托管的函数,在托管的函数里面干脏活,处理完成后,把结果抛到主线程。结果1-a: 5 - tid:22。结果1-b: 5 - tid:24。结果2-a: 9 - tid:22。https://blog.csdn.net/zhangphil/article/details/130161705

    https://zhangphil.blog.csdn.net/article/details/129250518https://zhangphil.blog.csdn.net/article/details/129250518

     

     

     

  • 相关阅读:
    nginx----(1)nginx的单机安装
    BFS:FloodFill算法
    SQL29 计算用户的平均次日留存率
    华为机试 - 数大雁
    广告和电商应该怎么串联起来呢?我们可以从各大巨头的动作中发掘
    如何用Java设计自动售货机?
    外卖市场繁荣背后,外卖员保障缺失的困境与突围
    Linux网络编程- ether_header & iphdr & tcphdr
    zk 系四大 L2 协议大 PK:进度、异同和生态
    第一章-扩散模型的基础知识
  • 原文地址:https://blog.csdn.net/zhangphil/article/details/130794990
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号