gpt4 book ai didi

kotlin - 如何在 Kotlin 中使用 volatile

转载 作者:IT老高 更新时间:2023-10-28 13:37:13 26 4
gpt4 key购买 nike

我尝试过这样的事情:

private volatile var instanceState: InstanceState = InstanceState.starts

Error: 'Expected member declaration'

像这样:

private volatile var instanceState: InstanceState = InstanceState.starts

Error: 'Property getter or setter expected'

Kotlin 不支持 Volatile ?

最佳答案

您可以使用 @Volatile 将属性标记为 volatile注释:

@Volatile var name:String = "stack"

生成的字段声明等同于 Java 的:

private volatile java.lang.String name;

根据 @Volatile文档:

Marks the JVM backing field of the annotated property as volatile, meaning that writes to this field are immediately made visible to other threads.

关于kotlin - 如何在 Kotlin 中使用 volatile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37852390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com