SYNCHRONIZED
Использует блокировку, чтобы гарантировать, что инициализировать экземпляр Lazy может только один поток, а также что инициализированное значение будет доступно всем потокам. Используемая блокировка является деталью, зависящей от платформы и реализации.
Примеры
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, and both threads will see 42 as an answer
t1.join()
t2.join()
//sampleEnd
}
Свойства
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-lazy-thread-safety-mode/-s-y-n-c-h-r-o-n-i-z-e-d/index.html