다음 코드가 있습니다.비동기 작업 중이지만 기다리는 중 참조를 얻지 못했습니다
왜 이런 일이 일어나는 지 이해할 수 없습니다. 나는 이것을 다루는 SO에 대한 기존의 답을 찾지 못했다. 나는 tenny와 twy가 할당 된 곳에서 직접 기다리는 것을 시도했으나 그것도 작동하지 않습니다.
aysnc가 작동하기 때문에 종속성에 문제가 있다고 생각하지 않습니다. build.gradle 파일도 게시했습니다. 당신의 star_two
및 star_ten
기능 Int
를 반환하기 때문에
import kotlinx.coroutines.experimental.async
fun main(args: Array<String>) {
async{
val tenny = star_ten(1)
val twy =star_two(10)
println()
println(twy.await()+tenny.await())
println()
}
}
fun star_two(num:Int):Int{
return num * 2
}
fun star_ten(num:Int):Int{
return num * 10
}
내 build.gradle 그렇게 tenny
및 twy
변수는 단지 Int
입니다, 당신은 await()
에 대한 해결되지 않은 참조를 얻고있다
group 'org.nul.cool'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
kotlin {
experimental {
coroutines 'enable'
}
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.19.2"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}