🧵Java Multithreading Programs
Concurrency questions separate junior from senior Java developers. These programs build up from creating a single thread to coordinating several, and every one is written to print the same output on each run, so the behaviour is easy to follow.20 programs · each with full code, output and a step-by-step explanation
Most Asked in Interviews
advanced
Print Odd and Even Numbers Using Two Threads
advancedProducer-Consumer Problem
advancedImplement a Thread-Safe Singleton
advancedAvoid Deadlock with Lock Ordering
advancedRun Tasks with ExecutorService
intermediateSynchronize a Shared Counter
intermediateWait for Workers Using CountDownLatch
intermediateCreate a Thread Using Runnable
🔍
Intermediate
01
View Program →Create a Thread by Extending Thread
Defines a worker by subclassing Thread, starts it, and waits for it with join.
intermediateConcurrency
02
View Program →Create a Thread Using Runnable
Wraps a task in a Runnable and hands it to a named Thread, keeping the task separate from the thread.
intermediateConcurrency
03
View Program →Interrupt a Sleeping Thread
Wakes a thread from a long sleep early by interrupting it, and handles the resulting InterruptedException.
intermediateConcurrency
04
View Program →Protect Shared Data Using ReentrantLock
Keeps a shared balance correct under two threads by guarding every deposit with a ReentrantLock.
intermediateConcurrency
05
View Program →Stop a Thread Using a volatile Flag
Stops a busy worker thread cleanly by setting a volatile boolean flag that the worker checks in its loop.
intermediateConcurrency
06
View Program →Synchronize a Shared Counter
Prevents lost updates on a counter shared by two threads with a synchronized method.
intermediateConcurrency
07
View Program →Wait for Threads Using join()
Splits a sum across two threads and uses join so main reads the results only after both finish.
intermediateConcurrency
08
View Program →Wait for Workers Using CountDownLatch
Makes the main thread wait until three start-up workers have all reported ready, using a CountDownLatch.
intermediateConcurrency
Advanced
09
View Program →Avoid Deadlock with Lock Ordering
Prevents a bank-transfer deadlock by always acquiring the two account locks in the same order.
advancedConcurrency
10
View Program →Combine Async Results with CompletableFuture
Runs two lookups asynchronously and combines their results with CompletableFuture.thenCombine.
advancedConcurrency
11
View Program →Count Words Concurrently Using ConcurrentHashMap
Counts word frequencies across three threads at once with ConcurrentHashMap.merge, without any explicit locks.
advancedConcurrency
12
View Program →Implement a Bounded Blocking Queue
Builds a fixed-capacity queue whose put waits when full and whose take waits when empty, using ReentrantLock and Conditions.
advancedConcurrency
13
View Program →Implement a Rate Limiter
Limits how often requests are accepted using the token bucket algorithm, allowing short bursts but a steady average rate.
advancedConcurrency
14
View Program →Implement a Thread Pool
Builds a fixed-size thread pool from scratch with worker threads that take tasks from a shared BlockingQueue.
advancedConcurrency
15
View Program →Implement a Thread-Safe Singleton
Creates exactly one instance of a class even when several threads request it at once, using double-checked locking.
advancedConcurrency
16
View Program →Print Odd and Even Numbers Using Two Threads
Makes two threads take turns printing 1 to 10, one printing odd numbers and the other even, with wait and notifyAll.
advancedConcurrency
17
View Program →Producer-Consumer Problem
Coordinates a producer and a consumer thread through a bounded BlockingQueue.
advancedConcurrency
18
View Program →Run Tasks with ExecutorService
Submits factorial tasks to a fixed thread pool and collects the results through Futures.
advancedConcurrency
19
View Program →Use the Fork/Join Framework to Sum an Array
Sums 100,000 numbers by recursively splitting the array into halves that run in parallel on a ForkJoinPool.
advancedConcurrency
20
View Program →Use a ReadWriteLock for a Shared Cache
Lets many threads read a shared price cache at once while writers get exclusive access, using ReentrantReadWriteLock.
advancedConcurrency