Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

High Performance Computing

Parallel streams, CompletableFuture, virtual threads, and concurrent collections for Java 21.

Contents

Class Topics
ParallelStreamExamples Parallel reduction, custom ForkJoinPool, groupingByConcurrent, ordered vs unordered
CompletableFutureExamples Async chaining (thenCompose, thenCombine), fan-out/fan-in (allOf), error handling, timeouts
ConcurrentCollectionsExamples ConcurrentHashMap (merge, search), BlockingQueue producer-consumer, LongAdder, Semaphore, StampedLock optimistic reads
VirtualThreadExamples Virtual thread creation, newVirtualThreadPerTaskExecutor, massive concurrency, structured concurrency pattern (Java 21+)

Key Guidelines

When to use parallel streams

  • Large datasets (10k+ elements) with CPU-bound operations
  • Stateless, non-interfering, associative operations
  • 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

  1. Measure first — use JMH for micro-benchmarks, not System.nanoTime()
  2. Avoid shared mutable state — prefer immutable data and thread-local accumulators
  3. Right-size thread pools — CPU-bound: Runtime.getRuntime().availableProcessors(), I/O-bound: higher
  4. Prefer LongAdder over AtomicLong under contention
  5. Use StampedLock optimistic reads for read-dominated workloads

Related Documentation