如何在Kotlin中使用volatile

如何在Kotlin中使用volatile,kotlin,Kotlin,我试过这样的方法: private volatile var instanceState: InstanceState = InstanceState.starts Error: 'Expected member declaration' private volatile var instanceState: InstanceState = InstanceState.starts Error: 'Property getter or setter expected' 就像这样: p

我试过这样的方法:

private volatile var instanceState: InstanceState = InstanceState.starts

Error:  'Expected member declaration'
private volatile var instanceState: InstanceState = InstanceState.starts

Error:  'Property getter or setter expected'
就像这样:

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?

Kotlin没有
Volatile
关键字,但有注释:
@Volatile
()

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

@Volatile var name:String = "stack"
生成的字段声明相当于Java的:

private volatile java.lang.String name;
根据文件:

将带注释属性的JVM支持字段标记为volatile, 这意味着对该字段的写入将立即对用户可见 其他线程


注释应该在同一行,Ithink@voddan这是推荐的Kotlin风格指南吗?