【问题标题】:Unresolved reference: async in Kotlin in 1.3未解决的参考:Kotlin 1.3 中的异步
【发布时间】:2019-06-14 10:33:42
【问题描述】:

我在 github here 有多模块 kotlin gradle 项目。

我的一个子项目介绍-coroutines 与构建文件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,但不知道为什么会出现此错误。

我已经验证了类似的问题here并添加了anko-commons依赖here

我该如何解决这个错误?

【问题讨论】:

    标签: kotlin coroutine kotlinx.coroutines kotlin-extension


    【解决方案1】:

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

    kotlin {
        experimental {
            coroutines   = Coroutines.ENABLE
        }
    }
    

    您不能再隐式使用async() 函数。您必须为全局作用域协程 GlobalScope.async(){...} 显式调用它,或者您可以使用 CoroutineScope(...).async{...} 从另一个协程作用域调用它,或者从作用域函数 coroutineScope {...}withContext(...){...} 调用它。

    我写了一个example 供个人使用,以了解协程是如何工作的。我希望它是好的和有用的。

    【讨论】:

    【解决方案2】:

    问题是async(与launch相同)被定义为CoroutineScope上的扩展函数。在下面的例子中,它在withContext的接收者CoroutineScope中被调用:

    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
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      你可以试试这个:

        CoroutineScope(Dispatchers.Default).async {
              ...
        }
      

      【讨论】:

        猜你喜欢
        • 2018-10-25
        • 1970-01-01
        • 1970-01-01
        • 2021-11-02
        • 2021-10-29
        • 2018-10-16
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多