SYNCHRONIZED
Uses a lock to ensure that only a single thread can initialize a Lazy instance, and ensures that initialized value is visible by all threads. The lock used is both platform- and implementation- specific detail.
Samples
import samples.assertPrints
import kotlin.concurrent.thread
fun main() {
//sampleStart
val answer: Int by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
42
}
val t1 = thread {
println("Thread 1: $answer")
}
val t2 = thread {
println("Thread 2: $answer")
}
// It is guaranteed that 'Computing' message will be printed once, but both threads will see 42 as an answer
t1.join()
t2.join()
//sampleEnd
}