Java Concurrency
Java With DSA — One-Page Study Document
Threads
Threads share heap memory while each thread has its own stack. Concurrency coordinates multiple tasks; parallelism executes tasks
simultaneously.
Race Condition
Occurs when correctness depends on timing. Operations such as count++ are compound operations and are not inherently atomic.
synchronized
Provides mutual exclusion and happens-before guarantees around monitor locking. Keep critical sections focused but correct.
volatile
Provides visibility and ordering guarantees but does not make compound operations atomic.
Atomic Classes
AtomicInteger, AtomicLong and related classes provide atomic operations using mechanisms such as compare-and-set.
ExecutorService
Use managed thread pools instead of creating unlimited threads. Understand pool sizing, task queues, shutdown and rejection policies.
CompletableFuture
Supports asynchronous composition. thenApply transforms results, thenCompose chains asynchronous stages, and
exceptionally/handle manage failures.
Deadlocks
Avoid circular lock acquisition and inconsistent lock ordering. Establish a predictable lock hierarchy when multiple locks are required.