Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 为什么线程的名称在加入协同路由作用域中的所有作业后发生更改?_Multithreading_Kotlin_Coroutine_Kotlin Coroutines - Fatal编程技术网

Multithreading 为什么线程的名称在加入协同路由作用域中的所有作业后发生更改?

Multithreading 为什么线程的名称在加入协同路由作用域中的所有作业后发生更改?,multithreading,kotlin,coroutine,kotlin-coroutines,Multithreading,Kotlin,Coroutine,Kotlin Coroutines,我有一段让我困惑的代码。为什么我要使用CoroutineScope(Dispatchers.IO)。在runBlocking中启动并用println()检查它。每次我测试它我的 println("01 Runblocking Scope Before join() => THis is ${Thread.currentThread().name} and ID is ${Thread.currentThread().id}") 将不同于 println("02 Runblocking S

我有一段让我困惑的代码。为什么我要使用CoroutineScope(Dispatchers.IO)。在runBlocking中启动并用println()检查它。每次我测试它我的

println("01 Runblocking Scope Before join() => THis is ${Thread.currentThread().name} and ID is ${Thread.currentThread().id}")
将不同于

println("02 Runblocking Scope Before join() => THis is ${Thread.currentThread().name} and ID is ${Thread.currentThread().id}")
这是我的密码

但是如果我从CoroutineScope(Dispatchers.IO).launch{}更改为this.launch{},那么前一行中的println()都将是相同的。我认为这是因为引用了相同的作用域(runBlocking{}),但为什么我使用CoroutineScope(Dispatchers.IO).launch{}那些println()不会打印相同的数据

runBlocking {
    println("Outside Scope => THis is ${Thread.currentThread().name} and ID is ${Thread.currentThread().id}")
    CoroutineScope(Dispatchers.IO).launch {
    //this.launch {
    println("Runblocking Scope => THis is ${Thread.currentThread().name} and ID is ${Thread.currentThread().id}")
        val jobs = mutableListOf<Job>()
        for( i in IntRange(0, 10)) {
            jobs.add(CoroutineScope(Dispatchers.IO).launch {
                printData(i)
            })
        }
        println("01 Runblocking Scope Before join() => THis is ${Thread.currentThread().name} and ID is ${Thread.currentThread().id}")

        jobs.joinAll()

        println("02 Runblocking Scope Before join() => THis is ${Thread.currentThread().name} and ID is ${Thread.currentThread().id}")
        println("Exit Runblocking Scope")
    }.join()
}
runBlocking{
println(“范围外=>这是${Thread.currentThread().name},ID是${Thread.currentThread().ID}”)
协同路由示波器(Dispatchers.IO)。启动{
//这次发射{
println(“Runblocking Scope=>这是${Thread.currentThread().name},ID是${Thread.currentThread().ID}”)
val jobs=mutableListOf()
用于(i在内部传输(0,10)){
添加(CoroutineScope(Dispatchers.IO).launch{
打印数据(一)
})
}
println(“01 Runblocking Scope Before join()=>这是${Thread.currentThread().name},ID是${Thread.currentThread().ID}”)
jobs.joinAll()
println(“02 Runblocking Scope Before join()=>这是${Thread.currentThread().name},ID是${Thread.currentThread().ID}”)
println(“退出运行阻塞范围”)
}.join()
}
作业。joinAll()在所有作业完成之前暂停执行。这并不意味着当前线程处于等待状态

将保存协同程序的状态,并将当前线程返回到线程池中

完成所有作业后,协同程序将使用线程池中的任何可用线程继续执行


这就是为什么您会看到不同的ID。然而,根据许多因素,行为可能会有所不同。例如,在我的机器上运行您的代码总是打印相同的ID。

谢谢,先生,您让我明白了我的困惑。:)