🧵 Concurrency Fundamentals
14 Java concepts, each explained step by step with real code, a common trap and practice questions.
- Processes vs threadsThreads share heap memory within a process; each has its own stack.
- Creating threadsThread + Runnable, Thread.ofPlatform, start() vs run().
- Thread lifecycleNEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED.
- sleep, join & interruptCooperative cancellation, InterruptedException, restoring the interrupt flag.
- Race conditionscount++ is read-modify-write and not atomic.
- synchronizedIntrinsic locks on methods and blocks, mutual exclusion, reentrancy.
- Visibility & volatileCaching across cores; volatile guarantees visibility but not atomicity.
- The Java Memory Modelhappens-before rules: locks, volatile, thread start/join, final fields.
- DeadlockCircular waiting on locks, and lock ordering to prevent it.
- Livelock & starvationThreads busy but not progressing; unfair scheduling.
- wait / notifyCondition waiting inside synchronized, spurious wakeups, while-loop guard.
- Daemon threadsDaemon threads don't keep the JVM alive.
- Thread safety strategiesImmutability, confinement, synchronization, thread-safe classes.
- ThreadLocalPer-thread values and leak risks in pools.