代码之家  ›  专栏  ›  技术社区  ›  user51

未解析的引用:1.3中Kotlin中的Async

  •  0
  • user51  · 技术社区  · 6 年前

    我在Github有多模块Kotlin Gradle项目 here .

    我的一个子项目引入了带有build文件build.gradle.kts的协程,它是 here

    build.gradle.kts的内容是-

        import org.jetbrains.kotlin.gradle.dsl.Coroutines
        import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
    
        plugins {
            java
            kotlin("jvm") version "1.3.11"
        }
    
        group = "chapter2"
        version = "1.0-SNAPSHOT"
    
        repositories {
            mavenCentral()
        }
    
        dependencies {
            compile(kotlin("stdlib-jdk8"))
            compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0")
            testCompile("junit", "junit", "4.12")
        }
    
        configure<JavaPluginConvention> {
            sourceCompatibility = JavaVersion.VERSION_1_8
            targetCompatibility = JavaVersion.VERSION_1_8
        }
    
        tasks.withType<KotlinCompile> {
            kotlinOptions.jvmTarget = "1.8"
        }
    
        kotlin {
            experimental {
                coroutines   = Coroutines.ENABLE
            }
        }
    

    我正在尝试用这个创建我的第一个协同程序 link .

    import kotlinx.coroutines.*
    import kotlinx.coroutines.async
    import kotlin.system.*
    import kotlin.system.measureTimeMillis
    
    suspend  fun computecr(array: IntArray, low: Int, high: Int): Long {
        return if (high - low <= SEQUENTIAL_THRESHOLD) {
            (low until high)
                    .map { array[it].toLong() }
                    .sum()
        } else {
            val mid = low + (high - low) / 2
            val left = async { computecr(array, low, mid) }
            val right = compute(array, mid, high)
            return left.await() + right
        }
    }
    

    当我编译程序时,得到以下错误-

    e: /Users/rajkumar.natarajan/Documents/Coding/coroutines-demo/introducing-coroutines/src/main/kotlin/SumUsingCoroutines.kt: (15, 20): Unresolved reference: async
    > Task :introducing-coroutines:compileKotlin FAILED
    
    FAILURE: Build failed with an exception.
    

    我可以进口 import kotlinx.coroutines.async 没有任何问题,但不知道为什么会出现这个错误。

    enter image description here

    我已经证实了类似的问题 here 并添加 anko-commons 附属国 here

    如何解决此错误?

    2 回复  |  直到 6 年前
        1
  •  2
  •   LiTTle    6 年前

    首先,您必须从Gradle中删除启用实验协同程功能的部分。

    kotlin {
        experimental {
            coroutines   = Coroutines.ENABLE
        }
    }
    

    不能使用 async() 函数已隐式化。您必须为全局范围协程显式地调用它 GlobalScope.async(){...} 或者你可以从另一个协程器调用它 CoroutineScope(...).async{...} 或从范围功能 coroutineScope {...} , withContext(...){...} .

    我写了一篇 example 以便于个人了解协同程序是如何工作的。我希望它是好的和有帮助的。

        2
  •  1
  •   Omar Mainegra    6 年前

    问题是 async (同) launch )定义为上的扩展函数 CoroutineScope . 在下面的示例中,它在接收器中被调用 鱼尾镜 属于 withContext :

    suspend  fun computecr(array: IntArray, low: Int, high: Int): Long {
        return if (high - low <= SEQUENTIAL_THRESHOLD) {
            (low until high)
                .map { array[it].toLong() }
                .sum()
        } else {
            withContext(Default) {
                val mid = low + (high - low) / 2
                val left = async { computecr(array, low, mid) }
                val right = computecr(array, mid, high)
                left.await() + right
            }
        }
    }