🐦 Kotlin Intermediate

What is lazy thread safety mode in Kotlin?

Answer

The lazy delegate supports three thread safety modes. SYNCHRONIZED (default) uses a lock to ensure only one thread initializes the value — safe for multi-threaded access but with synchronization overhead. PUBLICATION allows multiple threads to initialize the value concurrently, but only the first result is used — suitable when initialization is safe to run multiple times. NONE has no synchronization overhead and should only be used when you are certain the lazy property will only be accessed from a single thread. Example: val data by lazy(LazyThreadSafetyMode.NONE) { expensiveComputation() }.