public class Lazy<T> extends Object
This implementation ensures that the originally defined initialization code
is called at most once, even if there is concurrent access from multiple threads, unless the
initialization attempt causes a RuntimeException.
Once the value is established, unnecessary effort to synchronize competing* read accesses is avoided.
*Pure read accesses are of course not really competing.
| Modifier and Type | Class and Description |
|---|---|
static class |
Lazy.InitException
An unchecked exception type that may be thrown, when the initialization code of a
Lazy instance
causes a checked exception. |
| Modifier and Type | Method and Description |
|---|---|
T |
get()
Executes the originally defined initialization code once on the first call
and returns its result on that and every subsequent call without executing the initialization code again.
|
static <T> Lazy<T> |
init(Supplier<? extends T> initial)
|
static <T> Lazy<T> |
initEx(XSupplier<? extends T,?> initial)
|
public static <T> Lazy<T> init(Supplier<? extends T> initial)
Lazy instance giving a Supplier that defines the intended initialization of the
represented value.T - The result type of the initialisation code.initEx(XSupplier)public static <T> Lazy<T> initEx(XSupplier<? extends T,?> initial)
Lazy instance giving an XSupplier that defines the intended initialization of the
represented value. The initialization code may throw a checked exception. If so, it is caught, wrapped in an
Lazy.InitException, and rethrown.T - The result type of the initialisation code.init(Supplier)public final T get()
Copyright © 2023 Andreas Kluge-Kaindl, Bremen (de). All rights reserved.