Practicing multithreading and concurrency concepts from scratch. Covers thread lifecycle, synchronization, locks, classical problems, and the java.util.concurrent utilities.
- Creating threads by extending
Thread - Thread naming and priorities (
MIN_PRIORITY,MAX_PRIORITY) sleep,yield,interrupt- Daemon threads
- Race condition demo with a shared
Counter - Fixing race conditions using
synchronizedblocks - Using
jointo wait for threads to finish
supplyAsyncfor async non-blocking tasksthenApplyfor chaining transformationsorTimeoutandexceptionallyfor timeout handling and fallbackallOfandjointo wait on multiple futures
- Synchronizing multiple threads at a common barrier point
- Barrier action (runnable triggered when all threads arrive),
await,reset - Difference from
CountDownLatch— reusable and threads wait for each other
- Waiting for multiple services to finish using
Future.getvsCountDownLatch countDown,awaitwith timeout
- Raw threads vs
ExecutorService— performance comparison with thread pools Executors.newFixedThreadPool,newSingleThreadExecutor,newScheduledThreadPoolFuture—get,isDone,cancel,isCancelled, timeout withget(timeout, unit)CallablevsRunnablewith futuresinvokeAllwith timeout,invokeAnyScheduledExecutorService—schedule,scheduleAtFixedRate,scheduleWithFixedDelay
- Producer-Consumer using
waitandnotifyon a shared resource - Guarded blocks to coordinate between threads without busy waiting
ReentrantLock— basic usage, reentrancy with nested method callslock(),tryLock()with timeout,lockInterruptibly(),unlock()- Bank account withdrawal demo showing
tryLockwith timeout to avoid indefinite blocking - Fair vs unfair lock —
ReentrantLock(true)for FIFO ordering vs default unfair mode ReentrantReadWriteLock— concurrent reads with exclusive write locks
src/main/java/com/definit3/concurrency/
├── basic/ # Thread creation, lifecycle, priorities, daemon
├── synchronization/ # Shared state, race conditions, synchronized blocks
├── completablefuture/ # supplyAsync, thenApply, allOf, orTimeout, exceptionally
├── cyclicbarrier/ # CyclicBarrier, barrier action, reset
├── countdownlatch/ # CountDownLatch, await, countDown
├── executorframework/ # ExecutorService, Future, Callable, ScheduledExecutor
├── threadcommunication/ # wait, notify, producer-consumer
└── explicit/
├── lock/ # ReentrantLock, tryLock, lockInterruptibly
├── lockfairness/ # Fair vs unfair lock ordering
└── readwritelock/ # ReentrantReadWriteLock, concurrent reads with exclusive writes