上一页 1 ··· 32 33 34 35 36 37 38 39 40 ··· 186 下一页
摘要: 一,代码: 函数: //得到用户信息,演示指定dispatcher suspend fun getUserInfo(): String { logX("Before IO Context.") withContext(Dispatchers.IO) { logX("In IO Context.") 阅读全文
posted @ 2025-08-02 14:59 刘宏缔的架构森林 阅读(47) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button2.setOnClickListener { runBlocking { suspend fun getResult1(): String { delay(3000L) // 模拟耗时操作 return "Result1" } suspe 阅读全文
posted @ 2025-08-02 14:59 刘宏缔的架构森林 阅读(8) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button1.setOnClickListener { runBlocking { suspend fun download() { // 模拟下载任务 delay(1000L) } println("得到job") val job = launc 阅读全文
posted @ 2025-08-02 14:59 刘宏缔的架构森林 阅读(21) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button2.setOnClickListener { runBlocking(Dispatchers.IO) { repeat(3) { index -> launch { //println() repeat(3) { println("第"+ 阅读全文
posted @ 2025-08-02 14:59 刘宏缔的架构森林 阅读(59) 评论(0) 推荐(0)
摘要: 一,代码: 函数: //得到sequence fun getSequence() = sequence { println("添加 1") yield(1) println("添加 2") yield(2) println("添加 3") yield(3) println("添加 4") yield 阅读全文
posted @ 2025-08-02 14:59 刘宏缔的架构森林 阅读(32) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button1.setOnClickListener { runBlocking { val channel = Channel<Int>() //发送消息 launch { (1..3).forEach { channel.send(it) log 阅读全文
posted @ 2025-07-26 11:39 刘宏缔的架构森林 阅读(33) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button1.setOnClickListener { runBlocking { //给协程命名 val job = launch(CoroutineName("协cor1")) { val corName = coroutineContext[ 阅读全文
posted @ 2025-07-26 11:39 刘宏缔的架构森林 阅读(16) 评论(0) 推荐(0)
摘要: 一,协程调度器Coroutine Dispatcher Dispatchers.Main,它只在 UI 编程平台才有意义,在 Android、Swing 之类的平台上,一般只有 Main 线程才能用于 UI 绘制。这个 Dispatcher 在普通的 JVM 工程当中,是无法直接使用的。 Dispa 阅读全文
posted @ 2025-07-26 11:39 刘宏缔的架构森林 阅读(68) 评论(0) 推荐(0)
摘要: 一,生命周期示意图 引用自极客时间朱老师 二,代码: binding.button1.setOnClickListener { runBlocking { val job = launch { println("launch开始") delay(1000L) println("launch结束") 阅读全文
posted @ 2025-07-26 11:39 刘宏缔的架构森林 阅读(35) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button1.setOnClickListener { runBlocking { // 仅用于测试,生成环境不要使用这么简易的CoroutineScope val scope = CoroutineScope(Job()) scope.launc 阅读全文
posted @ 2025-07-26 11:38 刘宏缔的架构森林 阅读(29) 评论(0) 推荐(0)
上一页 1 ··· 32 33 34 35 36 37 38 39 40 ··· 186 下一页