android - 如何在 Kotlin 中获取协程的名称?

标签 android kotlin coroutine

我很好奇协程的内部工作suspended在主线程上。真正的问题是如何记录 suspended函数,它是主线程上的协程。执行的具体地点在哪里?是虚拟线程吗?

最佳答案

如果您正在谈论记录协程名称:

你可以通过

  • 为协程命名(如果您想要自定义名称):launch(CoroutineName("My-Coroutine"))
  • 在 IntelliJ 工具栏菜单中启用日志记录:运行 -> 编辑配置并添加
  • -Dkotlinx.coroutines.debug在虚拟机选项中。

    VM Option in Edit Configuration

    然后可以看到@My-Coroutine在日志中。

    在编辑配置更改后尝试以下代码:
    fun main() = runBlocking {
    println("   'runBlocking': I'm working in thread ${Thread.currentThread().name}")
    
    val job = launch(CoroutineName("my-custom-name")) {
        println("   'runBlocking': I'm working in thread ${Thread.currentThread().name}")
    
    }
    
    job.join()}
    

    结果:
    Result

    关于android - 如何在 Kotlin 中获取协程的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319799/

    相关文章:

    android - 如何将onClick,onLongClick和onTouch实现到一个 View

    android - Android通知不会弹出

    kotlin - Kotlin 尖括号中的接口(interface)是什么?

    multithreading - 如何在 Android 平台的 unity3d 中使用 c# 线程?

    c - 使用 setjmp() 和 longjmp() 创建斐波那契生成器序列时出错

    C++ 协程/Visual Studio : How can a generator call a function that yields values on its behalf?

    android - 如何更改 Android 中的状态栏图标颜色?

    android - 将gradle中的外部jar添加到Android项目

    android - 是否有适用于 Android 的 Application::onDestroy() 等效项?

    android - 事件处理程序在哪个线程上执行?