kotlin volatile
object TaskExecutor : Runnable {
@Volatile
private var shouldContinue = true
// @Volatile: changes to this field are immediately visible to all threads
override fun run() {
while (shouldContinue) {}
println("Done")
}
fun stop() {
shouldContinue = false
}
}
fun main() {
val thread = Thread(TaskExecutor)
thread.start()
TaskExecutor.stop()
thread.join()
}