lazy

expect fun <T> lazy(initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED. The lock used is both platform- and implementation- specific detail.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

Since Kotlin

1.0

Samples

import samples.assertPrints
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val answer: Int by lazy {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

println("What is the answer?")
// Will print 'Computing...' and then 42
println(answer) // 42
println("Come again?")
// Will just print 42
println(answer) // 42 
   //sampleEnd
}

expect fun <T> lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer and thread-safety mode.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

For LazyThreadSafetyMode.SYNCHRONIZED, the lock used is both platform- and implementation- specific detail.

Since Kotlin

1.0

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
}
import samples.assertPrints
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   class Answer(val value: Int, val computedBy: Thread = Thread.currentThread()) {
    override fun toString() = "Answer: $value, computed by: $computedBy"
}

val answer: Answer by lazy(LazyThreadSafetyMode.PUBLICATION) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    Answer(42)
}

val t1 = thread(name = "#1") {
    println("Thread 1: $answer")
}

val t2 = thread(name = "#2") {
    println("Thread 2: $answer")
}

// It is **not** guaranteed that 'Computing' message will be printed once,
// but guaranteed that both threads will see 42 computed by *the same* thread as an answer
t1.join()
t2.join() 
   //sampleEnd
}

expect fun <T> lazy(lock: Any?, initializer: () -> T): Lazy<T>(source)

Deprecated

Warning since 1.9

Error since 2.1

Synchronization on Any? object is supported only in Kotlin/JVM.

Replace with

lazy(initializer)

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The lock parameter is ignored.

Since Kotlin

1.0
actual fun <T> lazy(initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer.

Since Kotlin

1.1

actual fun <T> lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The mode parameter is ignored.

Since Kotlin

1.1

actual fun <T> lazy(lock: Any?, initializer: () -> T): Lazy<T>(source)

Deprecated

Warning since 1.9

Error since 2.1

Synchronization on Any? object is not supported.

Replace with

lazy(initializer)

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The lock parameter is ignored.

Since Kotlin

1.1
actual fun <T> lazy(initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

The returned instance uses itself to synchronize on. Do not synchronize from external code on the returned instance as it may cause accidental deadlock. This behavior might be changed in the future.

Since Kotlin

1.0

Samples

import samples.assertPrints
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val answer: Int by lazy {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

println("What is the answer?")
// Will print 'Computing...' and then 42
println(answer) // 42
println("Come again?")
// Will just print 42
println(answer) // 42 
   //sampleEnd
}

actual fun <T> lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer and thread-safety mode.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

When the LazyThreadSafetyMode.SYNCHRONIZED mode is specified, the returned instance uses itself to synchronize on. Do not synchronize from external code on the returned instance as it may cause accidental deadlock. This behavior might be changed in the future.

Since Kotlin

1.0

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
}
import samples.assertPrints
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   class Answer(val value: Int, val computedBy: Thread = Thread.currentThread()) {
    override fun toString() = "Answer: $value, computed by: $computedBy"
}

val answer: Answer by lazy(LazyThreadSafetyMode.PUBLICATION) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    Answer(42)
}

val t1 = thread(name = "#1") {
    println("Thread 1: $answer")
}

val t2 = thread(name = "#2") {
    println("Thread 2: $answer")
}

// It is **not** guaranteed that 'Computing' message will be printed once,
// but guaranteed that both threads will see 42 computed by *the same* thread as an answer
t1.join()
t2.join() 
   //sampleEnd
}

actual fun <T> lazy(lock: Any?, initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

The returned instance uses the specified lock object to synchronize on. When the lock is not specified the instance uses itself to synchronize on, in this case do not synchronize from external code on the returned instance as it may cause accidental deadlock. This behavior might be changed in the future.

Since Kotlin

1.0

Samples

import samples.assertPrints
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val lock = Any()
val answer: Int by lazy(lock) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

// Lock is acquired first, so thread cannot compute the answer
val thread: Thread
synchronized(lock) {
    thread = thread(name = "#1") {
        println("Thread is asking for an answer")
        println("$answer")
    }
    println("Let's hold the thread #1 for a while with a lock")
    Thread.sleep(100) // Let it wait
}
// Lock is unlocked, the thread will print an answer
thread.join() 
   //sampleEnd
}
actual fun <T> lazy(initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

Note that the returned instance uses itself to synchronize on. Do not synchronize from external code on the returned instance as it may cause accidental deadlock. This behavior might be changed in the future.

Since Kotlin

1.3

Samples

import samples.assertPrints
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val answer: Int by lazy {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

println("What is the answer?")
// Will print 'Computing...' and then 42
println(answer) // 42
println("Come again?")
// Will just print 42
println(answer) // 42 
   //sampleEnd
}

actual fun <T> lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer and thread-safety mode.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

Since Kotlin

1.3

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
}
import samples.assertPrints
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   class Answer(val value: Int, val computedBy: Thread = Thread.currentThread()) {
    override fun toString() = "Answer: $value, computed by: $computedBy"
}

val answer: Answer by lazy(LazyThreadSafetyMode.PUBLICATION) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    Answer(42)
}

val t1 = thread(name = "#1") {
    println("Thread 1: $answer")
}

val t2 = thread(name = "#2") {
    println("Thread 2: $answer")
}

// It is **not** guaranteed that 'Computing' message will be printed once,
// but guaranteed that both threads will see 42 computed by *the same* thread as an answer
t1.join()
t2.join() 
   //sampleEnd
}

actual fun <T> lazy(lock: Any?, initializer: () -> T): Lazy<T>(source)

Deprecated

Error since 1.9

Hidden since 2.1

Synchronization on Any? object is not supported.

Replace with

lazy(initializer)

Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

The returned instance uses the specified lock object to synchronize on.

Since Kotlin

1.3
actual fun <T> lazy(initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer.

Since Kotlin

1.8

actual fun <T> lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The mode parameter is ignored.

Since Kotlin

1.8

actual fun <T> lazy(lock: Any?, initializer: () -> T): Lazy<T>(source)

Deprecated

Warning since 1.9

Error since 2.1

Synchronization on Any? object is not supported.

Replace with

lazy(initializer)

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The lock parameter is ignored.

Since Kotlin

1.8
actual fun <T> lazy(initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer.

Since Kotlin

1.8

actual fun <T> lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T>(source)

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The mode parameter is ignored.

Since Kotlin

1.8

actual fun <T> lazy(lock: Any?, initializer: () -> T): Lazy<T>(source)

Deprecated

Warning since 1.9

Error since 2.1

Synchronization on Any? object is not supported.

Replace with

lazy(initializer)

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The lock parameter is ignored.

Since Kotlin

1.8