lazy:-
- It represents a value with lazy initialization.
- Gets the lazily initialized value of the current Lazy instance. Once the value was initialized it must not change during the rest of the lifetime of this Lazy instance.
- Creates a new instance of the Lazy that uses the specified initialization function 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 the next access.
lazy returns a Lazy object that handles the lambda function(initializer) performing the initialization in a slightly different way according to the thread execution mode(LazyThreadSafetyMode).
public actual fun <T> lazy(mode: LazyThreadSafetyMode, initializer: ()
-> T): Lazy<T> =
when (mode) {
LazyThreadSafetyMode.SYNCHRONIZED -> SynchronizedLazyImpl(initializer)
LazyThreadSafetyMode.PUBLICATION -> SafePublicationLazyImpl(initializer)
LazyThreadSafetyMode.NONE -> UnsafeLazyImpl(initializer)
}
lazyFast:-
- Implementation of lazy that is not thread-safe. Useful when you know what thread you will be executing on and are not worried about
synchronization.
lazyFast also returns a Lazy object with mode LazyThreadSafetyMode.NONE
fun <T> lazyFast(operation: () -> T): Lazy<T> = lazy(LazyThreadSafetyMode.NONE) {
operation()
}
LazyThreadSafetyMode.SYNCHRONIZED:-
- Locks are used to ensure that only a single thread can initialize the Lazy instance.
LazyThreadSafetyMode.PUBLICATION:-
- Initializer function can be called several times on concurrent access to uninitialized Lazy instance value, but only the first returned value will be used as the value of Lazy instance.
LazyThreadSafetyMode.NONE :-
- No locks are used to synchronize access to the Lazy instance value; if the instance is accessed from multiple threads, its behavior is undefined. This mode should not be used unless the Lazy instance is guaranteed never to be initialized from more than one thread.
lazy
is a function from the standard library that instantiates aLazy
object.lazyFast
is not, so it can be virtually anything. – Perlite