A hands-on collection of Java concurrency, multithreading, and performance-tuning examples. This repository demonstrates core concurrency concepts, thread signaling patterns, high-concurrency bottlenecks, deadlock mitigation strategies, and modern features like Virtual Threads.
- Java Virtual Thread Example
- Demonstrates lightweight, high-throughput concurrency using Project Loom / Virtual Threads (
Executors.newVirtualThreadPerTaskExecutor()) vs. platform threads.
- Demonstrates lightweight, high-throughput concurrency using Project Loom / Virtual Threads (
- Java Deadlock
- Demonstrates how circular waiting and mismatched lock ordering lead to thread deadlocks.
- Java Deadlock Prevention - Timeout Backoff
- Shows how to prevent permanent deadlocks using
ReentrantLock.tryLock()with time-outs and exponential backoff.
- Shows how to prevent permanent deadlocks using
- Java False Sharing Example
- Demonstrates how multiple threads modifying adjacent variables on the same CPU cache line cause cache invalidation thrashing, and how to resolve it using
@Contendedpadding.
- Demonstrates how multiple threads modifying adjacent variables on the same CPU cache line cause cache invalidation thrashing, and how to resolve it using
- Java Thread Congestion - Shared BlockingQueue Example
- Illustrates performance bottlenecks when multiple producer and consumer threads contend for a single, shared
BlockingQueue.
- Illustrates performance bottlenecks when multiple producer and consumer threads contend for a single, shared
- Java Thread Congestion - Separate BlockingQueue Example
- Resolves queue contention by partitioning workloads across distinct queues (e.g., actor model or single-producer/single-consumer lanes).
- Java Thread Signaling Example 1 (Basic
wait()/notify())- Low-level signaling using intrinsic monitors and
Object.wait()/Object.notify().
- Low-level signaling using intrinsic monitors and
- Java Thread Signaling Example 2 (
ConditionVariables)- Modern signaling using
ReentrantLockand explicitConditioninstances (await()/signal()).
- Modern signaling using
- Java Thread Signaling Example 3 (
CountDownLatch)- One-shot barrier signaling allowing one or more threads to wait until a set of operations completes.
- Java Thread Signaling Example 4 (
Phaser/CyclicBarrier)- Reusable, phased barrier signaling for multi-stage concurrent task coordination.
- Java ForkJoinPool Example 1 (Divide and Conquer with
RecursiveTask)- Parallelizing computational workloads using work-stealing algorithms to compute results.
- Java ForkJoinPool Example 2 (
RecursiveAction& Custom Thread Pools)- Side-effect, result-less parallel processing and executing tasks on isolated
ForkJoinPoolinstances.
- Side-effect, result-less parallel processing and executing tasks on isolated
- JDK 21+ (Required for Virtual Threads)