Answers for "kotlin volatile"

0

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()
}
Posted by: Guest on April-21-2021

Browse Popular Code Answers by Language