Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volatile properties in Kotlin?

Tags:

kotlin

How does one mark a var in Kotlin volatile?

volatile public var tmpEndedAt: Long? = null

Is giving me the error: "unresolved reference: volatile".

like image 541
Andrew Cholakian Avatar asked Jun 08 '15 14:06

Andrew Cholakian


People also ask

What is volatile variable in Kotlin?

According to the Kotlin documentation Kotlin-@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. So, in Kotlin you can mark the property as volatile with @Volatile annotation.

Why is volatile keyword used?

The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time.

What is synchronized in Kotlin?

Marks the JVM method generated from the annotated function as synchronized , meaning that the method will be protected from concurrent execution by multiple threads by the monitor of the instance (or, for static methods, the class) on which the method is defined.


1 Answers

I decided to give Kotlin a shot by just using the "convert java to kotlin" function in IntelliJ. Apparently that set things up wrong.

I tried doing the same thing, but after applying the Kotlin Gradle plugin and placing the file in src/kotlin and it all worked. Thanks for the help anyway guys.

The code would be:

@Volatile var tmpEndedAt: Long? = null 
like image 168
Andrew Cholakian Avatar answered Oct 13 '22 01:10

Andrew Cholakian