Files

23 lines
1.1 KiB
Plaintext

$ java --enable-preview LazyDemo (JDK 27)
--- a lazy constant computes once, on first use
toString before first get(): jdk.internal.lang.LazyConstantImpl@...[computing function=LazyDemo$$Lambda/0x...@...]
(loading settings on main)
toString after first get(): jdk.internal.lang.LazyConstantImpl@...[Settings[region=ap-south-1, poolSize=16]]
CHECK ok : get() returns the same instance every time
CHECK ok : the supplier ran exactly once
--- and once even when 64 threads race for it
CHECK ok : 64 racing threads triggered exactly one initialisation
--- List.ofLazy: each element is computed on first access
computed so far: 0
squares.get(3) = square(3)=9
squares.get(3) = square(3)=9 (second read)
CHECK ok : only element 3 was ever computed
--- Map.ofLazy: the keys are fixed, the values are computed on first access
lengths.get("gamma") = 5
CHECK ok : only the requested key's value was computed
--- Set.ofLazy (new in 27): membership is decided by a predicate, on demand
primes.contains(7) = true, primes.contains(6) = false
CHECK ok : the lazy set answers membership using the predicate
primes = [2, 3, 5, 7]
exit=0