1.3 版本的 Kotlin 中出现未解决的引用:async

12

我在Github上有一个多模块的Kotlin Gradle项目,在这里

其中一个子项目introducing-coroutines的构建文件build.gradle.kts可以在这里找到。

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

我正试图从这个链接创建我的第一个协程程序。

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,但不确定为什么会出现此错误。

输入图像描述

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

如何解决这个错误?

3个回答

18

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

kotlin {
    experimental {
        coroutines   = Coroutines.ENABLE
    }
}

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

我写了一个示例,以便个人理解协程的工作方式。希望对你有所帮助。


在这里已经提到:“强烈不建议在GlobalScope实例上使用async或launch。” https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-global-scope/ - Spring
1
@Bunthai Deng 我同意这个观点。我只是写出了我所知道的每种可能的启动协程的方式!我没有提到好的或坏的实践! - LiTTle

5
问题在于,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
        }
    }
}

2
你可以尝试这样做:
  CoroutineScope(Dispatchers.Default).async {
        ...
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接