How to use volatile on Kotlin

I tried something like this:

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

and like this:

 private volatile var instanceState: InstanceState = InstanceState.starts Error: 'Property getter or setter expected' 

Problem not supported on Kotlin?

+7
kotlin
source share
2 answers

You can mark a property as volatile with @Volatile annotations:

 @Volatile var name:String = "stack" 

The resulting field declaration is equivalent to Java:

 private volatile java.lang.String name; 

According to @Volatile docs:

Marks the JVM support field of the annotated property as volatile, so that writing to this field immediately becomes visible to other topics.

+12
source share

Kotlin does not have the volatile keyword, but annotations: @Volatile ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-volatile/ )

0
source share

All Articles