SuspendLazy

class SuspendLazy<T>

A value initialized on first get, with one shared initialization across concurrent callers.

The initializer runs in the context of the first caller. A failed initialization is not cached: the failure propagates to that caller and the next get runs the initializer again. After a successful initialization the initializer reference is dropped and every later get returns without suspending.

Thread safety: this class is safe for concurrent use from multiple coroutines and threads; initialization is guarded by a Mutex and the stored value by a volatile field.

Properties

Link copied to clipboard

Returns true once a value has been successfully initialized.

Functions

Link copied to clipboard
suspend fun get(): T

Returns the value, running the initializer on the first call.

Link copied to clipboard
fun valueOrNull(): T?

Returns the initialized value, or null when no value exists yet, without initializing.