0 day streak
Code library

Expert Java exercises

Read these Expert Java snippets line by line — each one comes with a written breakdown of what the code does and why.

Java · Expert

Optimistic Locking with StampedLock

StampedLock offers an 'optimistic' mode where no actual lock is acquired. Instead, it returns a stamp that must be validated later. If a write occurred during the read, the validation fails and the…

PerformanceAsync & Concurrency
Open snippet →
Java · Expert

Fine-grained Memory Barriers with VarHandles

VarHandles provide a standard way to perform atomic operations and memory fencing. 'setRelease' ensures that all prior writes in the current thread are visible to other threads performing an 'acqui…

PerformanceAsync & Concurrency
Open snippet →
Java · Expert

Dynamic Invocation with MethodHandles

MethodHandles are the core mechanism behind 'invokedynamic'. They provide a more performant and type-safe alternative to traditional Reflection. Once a MethodHandle is linked, the JVM can optimize…

PerformanceOOP
Open snippet →
Java · Expert

Efficient Stack Walking with StackWalker API

The StackWalker API is a performance-oriented way to inspect the call stack. Unlike Throwable.getStackTrace(), it provides a stream-based approach that lazily fetches only the required frames, maki…

PerformanceError HandlingBest Practices
Open snippet →
Java · Expert

Virtual Thread Pinning and ReentrantLock

Virtual threads can be 'pinned' to their carrier platform thread if they block inside a 'synchronized' block or call native methods. To maintain high throughput and scalability, developers should r…

Async & ConcurrencyPerformanceBest Practices
Open snippet →
Java · Expert

Nested Record Patterns for Data Extraction

Record patterns (Java 21) allow for the deep deconstruction of complex data structures directly within 'instanceof' or 'switch' statements. This eliminates boilerplate casting and accessor calls, m…

SyntaxOOPBest Practices
Open snippet →
Java · Expert

Scoped Values for Lightweight Data Sharing

Scoped Values (JEP 446) provide a modern, thread-safe alternative to ThreadLocal. They are designed for virtual threads and allow for the efficient, immutable sharing of data across method boundari…

Async & ConcurrencyMemory ManagementBest Practices
Open snippet →
Java · Expertspring

Custom Thread-Local Scope Implementation

Custom Scopes allow developers to manage bean lifecycles beyond 'singleton' or 'prototype'. A ThreadScope is an expert pattern used in high-concurrency scenarios to ensure state is isolated per exe…

FrameworksMemory ManagementDesign Patterns
Open snippet →