简体   繁体   English

Kotlin withTimeout 在 Intellij gradle 项目中不起作用

[英]Kotlin withTimeout does not work in Intellij gradle project

Run following code give me the wrong results:运行以下代码给我错误的结果:

code代码

import kotlinx.coroutines.*

fun timeElapsed(nano: Long, msg: String){
    println("$msg milliseconds: ${(System.nanoTime()-nano)/1000000}")
}
suspend fun main(): Unit = coroutineScope {
    val start = System.nanoTime()
    withTimeout(700) { 
        println("in timout")
        timeElapsed(start, "timeout before delay")
        delay(900)
        println("not show?")
    }
}

wrong result错误的结果

in timout
timeout before delay milliseconds: 16
not show?
Exception in thread "main" kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 700 ms

run on https://play.kotlinlang.org/ give me the expected result:https://play.kotlinlang.org/上运行给我预期的结果:

in timout
timeout before delay milliseconds: 27
Exception in thread "main" kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 700 ms

My gradle setup我的 gradle 设置

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.6.21'
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.21'
}

group 'intro-coroutines'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib"
    implementation "org.jetbrains.kotlin:kotlin-reflect"
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2")

    def coroutines_version = '1.6.1'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-swing:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutines_version"

Intellij version: Build #IU-221.5080.210, built on April 11, 2022 Intellij 版本:Build #IU-221.5080.210,于 2022 年 4 月 11 日构建

kotline plugin version: 221-1.7.0-Beta-release-135-IJ5080.210 kotline 插件版本:221-1.7.0-Beta-release-135-IJ5080.210

Java SDK: JavaSE-1.8 Java SDK:JavaSE-1.8

what could possibly go wrong? go 可能有什么问题?

finally, I figured out why, totally my fault:最后,我弄清楚了原因,完全是我的错:

I define a delay() function in another Kotlin file in the same package as follows:我在同一个 package 的另一个 Kotlin 文件中定义了一个 delay() function 如下:

suspend fun delay(timeMillis: Long): Unit =
    suspendCoroutine { cont ->
        executor.schedule({
            cont.resume(Unit)
        }, timeMillis, TimeUnit.MILLISECONDS)
    }

As this continuation is a non-cancellable one, so delay is not cancellable, Sorry: lesson learned, function should be private, and check the import!由于此延续是不可取消的,因此延迟不可取消,对不起:吸取教训,function 应该是私有的,并检查导入!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntelliJ 项目“JS 客户端和 JVM 服务器 | Gradle”不起作用 - IntelliJ Project "JS Client and JVM Server | Gradle" does not work 如何使用 Gradle 和 IntelliJ 在 Kotlin 多平台项目中配置 JUnit 5? - How to configure JUnit 5 in a Kotlin multiplatform project using Gradle and IntelliJ? IntelliJ Kotlin多平台项目Gradle同步时间很长 - IntelliJ Kotlin multiplatform project Gradle sync very long Kotlin 使用 gradle Intellij 构建未使用新控制台应用程序项目构建 - Kotlin build not building with fresh console application project using gradle Intellij intellij中不推荐使用的ReplaceWith在Kotlin中如何工作? - How does Deprecated ReplaceWith work for Kotlin in intellij? 为什么 Intellij 不为新的 kotlin 项目创建 build.gradle.kts? - Why is Intellij not creating a build.gradle.kts for a new kotlin project? 使用 Gradle Kotlin DSL 安装依赖项不起作用 - Installing dependencys with Gradle Kotlin DSL does not work 如何在用 Kotlin 编写的 Intellij IDEA Gradle 插件项目中包含 Kotlin PSI 类(例如 KtClass)? - How to include Kotlin PSI classes (e.g. KtClass) in Intellij IDEA Gradle plugin project written in Kotlin? Intellij gradle kotlin 项目在 java 和 kotlin 目录中生成 .class 文件 - Intellij gradle kotlin project generate .class files in both java and kotlin directory Kotlin - IntelliJ 项目设置 - Kotlin - IntelliJ Project Setup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM