You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use a custom ForkJoinPool in server apps to avoid starving the common pool
When to use CompletableFuture
Composing multiple async I/O operations
Fan-out/fan-in patterns (call multiple services in parallel)
When you need fine-grained error handling and timeouts
When to use virtual threads (Java 21+)
I/O-bound workloads with high concurrency (HTTP servers, DB queries)
Thread-per-request architecture
Avoid for CPU-bound work — use parallel streams or ForkJoinPool instead
Concurrent collections cheat sheet
Need
Use
Thread-safe map with atomic updates
ConcurrentHashMap
Read-heavy, write-rare list
CopyOnWriteArrayList
Producer-consumer queue
ArrayBlockingQueue / LinkedBlockingQueue
High-contention counter
LongAdder (not AtomicLong)
Rate limiting / resource pooling
Semaphore
Optimistic read-heavy locking
StampedLock
How to Run
# From project root (Java 21 — VirtualThreadExamples compiles on the default toolchain)
./mvnw -pl examples/hpc compile
# Run tests
./mvnw -pl examples/hpc test# Optional: compiling this module with JDK 17 activates the `java17` overlay,# which excludes VirtualThreadExamples.java. The root POM is already Java 21.
Performance Tips
Measure first — use JMH for micro-benchmarks, not System.nanoTime()
Avoid shared mutable state — prefer immutable data and thread-local accumulators