kotlin协程执行优先级

kotlin协程执行优先级,kotlin,kotlin-coroutines,Kotlin,Kotlin Coroutines,我需要帮助理解以下两段代码的结果 第一个片段 fun main() = runBlocking { launch { println("Task from runBlocking") } coroutineScope { launch { println("Task from nested launch") } println("Task from coroutine scop

我需要帮助理解以下两段代码的结果

第一个片段

fun main() = runBlocking { 
    launch {
        println("Task from runBlocking")
    }

    coroutineScope { 
        launch {
            println("Task from nested launch")
        }

        println("Task from coroutine scope")
    }

    println("Coroutine scope is over")
}
fun main() = runBlocking { 
    launch {
        println("Task from runBlocking")
    }

    println("Coroutine scope is over") 
}
和,第二段

fun main() = runBlocking { 
    launch {
        println("Task from runBlocking")
    }

    coroutineScope { 
        launch {
            println("Task from nested launch")
        }

        println("Task from coroutine scope")
    }

    println("Coroutine scope is over")
}
fun main() = runBlocking { 
    launch {
        println("Task from runBlocking")
    }

    println("Coroutine scope is over") 
}
第一个片段的结果是:

Task from coroutine scope
Task from runBlocking
Task from nested launch
Coroutine scope is over
Coroutine scope is over
Task from runBlocking
第二个片段的结果是:

Task from coroutine scope
Task from runBlocking
Task from nested launch
Coroutine scope is over
Coroutine scope is over
Task from runBlocking

所以,问题是为什么要按这种顺序打印语句?

对于您正在使用的
coroutineScope
,正如您从中看到的,它被定义为一个
suspend
函数,因此它阻止了当前线程,并且
Coroutine作用域已结束
直到
coroutineScope
块完成后才会打印

对于
2nd snippet
,字符串
“Coroutine scope is over”
“Task from run blocking”
之前打印,因为它的println在主线程中执行,而在工作线程中运行的
launch
尚未完成其作业

fun mainOne() = runBlocking {
  launch {
      println("Task from runBlocking")
  }

  // coroutineScope is defined as a suspend function, so it's blocking the current thread
  coroutineScope {

      launch {
          // worker thread, slower than main thread, printed after "Task from coroutine scope"
          println("Task from nested launch")
      }
      // main thread, printed before "Task from nested launch" within CoroutineScope
      println("Task from coroutine scope")
  }

  // main thread is now free to run and it prints the string below
  println("Coroutine scope is over")
}


fun mainTwo() = runBlocking {
    launch {
        // worker thread, slower than main thread, printed after "Coroutine scope is over" due to concurrency
        println("Task from runBlocking")
    }
    // main thread is running and prints the string below before launch job has finished.
    // If you put a delay here you'll notice that launch job gets completed before "Coroutine scope is over"
    // E.g delay(2000)
    println("Coroutine scope is over")
}

希望这是有意义的:)

调用
launch
只会将任务放入队列,而调用
coroutineScope
会暂停当前的coroutine,直到新的coroutineScope完成

这就是代码段2中发生的情况:

  • 将“来自运行阻塞的任务”置于队列上

  • 打印“协同程序范围已结束”

  • 执行队列中的唯一任务(打印“运行阻塞中的任务”)

这就是代码段1中发生的情况

  • 将“来自运行阻塞的任务”置于队列上
  • 调用了coroutineScope(一个挂起函数)!立即调用suspend函数,直到suspend函数完成,mainCoroutine才会继续
  • 将“来自嵌套启动的任务”置于队列上
  • 打印“来自协同程序作用域的任务”
  • 暂停执行,因为在队列中的子对象完成之前,coroutineScope无法完成
  • 队列中的第一个任务执行“来自runBlocking的任务”
  • 队列中的第二个任务执行“来自嵌套启动的任务”
  • 新的CoroutineScope已完成,因为其子任务刚刚完成!返回主协同程序
  • 打印“协同程序范围已结束”

他们不必这样做,唯一的问题是一旦coruotine作用域关闭,其所有的协同程序都将终止。因此,在第一个示例中,
coroutineScope
中的语句后面是“coroutineScope is over”。在第二个示例中,语句
coroutineScope is over
是一个谎言。您可以在coroutine范围内打印它。嘿@MarkoTopolnik,您可以详细说明一下吗?这是什么意思?
runBlocking
建立了您所指的协同路由范围,对吗?当你打印
Coroutine scope is over
@afairplayer时还没有结束你试过我的答案了吗?我认为这个答案不正确。在不重写调度程序的情况下,所有这些任务都在主线程上运行