Articles

Articles by Daniel Hinojosa

Stable Values

Java’s upcoming Stable Values (soon to be renamed Lazy Constants in JEP 526) introduce a long-awaited mechanism for lazy initialization with memoization — values that are computed once, on demand, and cached for future access.

This article walks you through how to use StableValue to efficiently manage the creation of expensive or heavy objects, comparing it to older Java approaches such as factories and suppliers. Using clear JShell examples, it demonstrates how StableValue can simplify lazy initialization for timestamps, collections (list, map), and functions — all while ensuring thread safety and immutability.

Beyond simple examples, the article explores real-world applications:

  • Dependency Injection without Spring or Quarkus

  • Lazy Logger initialization

  • Thread-safe Singletons

  • Flyweight pattern caching

Through practical demonstrations, the post illustrates how StableValue (or LazyConstant) enhances the performance, predictability, and elegance of Java code, aligning with Java’s ongoing evolution toward immutability and functional-style programming.

Virtual Threads and Structured Concurrency

Virtual Threads and Structured Concurrency are complementary features from Project Loom that simplify concurrency in Java. Virtual Threads** make concurrency cheap and scalable. They allow a “thread-per-task” style without exhausting OS resources. Structured Concurrency makes concurrency safe and predictable. It groups concurrent tasks into a scope so their lifetimes, errors, and cancellations are managed together.