本文主要是介绍Kotlin协程CoroutineScope命名空间CoroutineName,Kotlin,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Kotlin协程CoroutineScope命名空间CoroutineName,Kotlin
import kotlinx.coroutines.*fun main(args: Array<String>) {val myName = CoroutineName("fly")runBlocking {CoroutineScope(Dispatchers.IO).launch {repeat(3) {val name = coroutineContext[CoroutineName]?.nameprintln("$name - $it") //默认无命名的协程。}}CoroutineScope(Dispatchers.IO + myName).launch {repeat(3) {val str = coroutineContext[CoroutineName]?.nameprintln("$str - $it")}}}
}
null - 0
null - 1
null - 2
fly - 0
fly - 1
fly - 2
kotlin协程coroutineScope-CSDN博客文章浏览阅读323次。coroutineScope 创建独立协程作用域,直到所有启动的协程都完成后才结束自己。runBlocking 和 coroutineScope 很像,它们都需要等待内部所有相同作用域的协程结束后才会结束自己。两者主要区别是: runBlocking 阻塞当前线程,而 coroutineScope不会,coroutineScope会挂起并释放底层线程供其它协程使用。kotlin协程coroutineScope。https://blog.csdn.net/zhangphil/article/details/129265638
这篇关于Kotlin协程CoroutineScope命名空间CoroutineName,Kotlin的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!