Concurrent Collections and Atomics

Java provides ConcurrentHashMap, CopyOnWriteArrayList, BlockingQueue, and atomic classes such as AtomicInteger and AtomicReference.

ConcurrentHashMap<String, LongAdder> counters =
    new ConcurrentHashMap<>();

counters.computeIfAbsent("java", key -> new LongAdder())
        .increment();

Concurrent collections provide well-defined concurrent operations. LongAdder can outperform AtomicLong under heavy contention because updates are distributed across cells.

Key Takeaways
  • Use concurrent collections rather than manually synchronizing ordinary collections when appropriate.
  • Atomics support thread-safe single-variable state transitions.
  • Contention characteristics should guide counter selection.