Java Multithreading Interview Questions
Concurrency questions test whether you understand visibility, atomicity and ordering, not just the API. These answers cover the multithreading topics Java interviews commonly return to.
Threads and synchronisation
What is the difference between Runnable, Callable and Future?
Runnable.run() returns nothing and cannot throw checked exceptions. Callable.call() returns a value and may throw. Submitting a Callable to an ExecutorService returns a Future, which lets you wait for or cancel the result. CompletableFuture builds on this with non-blocking composition.
synchronized vs ReentrantLock?
synchronized is simple and releases the monitor automatically, including on exceptions. ReentrantLock must be unlocked in a finally block but adds tryLock with timeout, interruptible lock acquisition, optional fairness and multiple Condition objects.
lock.lock();
try {
// critical section
} finally {
lock.unlock();
}What does volatile guarantee?
A write to a volatile variable is visible to later reads from other threads, and it prevents certain reorderings. It does not make compound actions atomic: count++ on a volatile int is still a race. Use AtomicInteger or a lock for that.
What is the difference between wait() and sleep()?
wait() must be called while holding the object's monitor, releases it while waiting, and resumes after notify()/notifyAll() or a timeout. Thread.sleep() just pauses the current thread and keeps any locks it holds. Always call wait() in a loop that rechecks the condition.
Deadlocks and thread pools
What causes a deadlock and how do you prevent it?
Deadlock needs mutual exclusion, hold-and-wait, no pre-emption and a circular wait. Break one condition: acquire locks in a consistent global order, hold fewer locks, or use tryLock with a timeout and back off. Take a thread dump (jstack or jcmd Thread.print) to confirm; the JVM reports deadlocked monitors.
How does ThreadPoolExecutor decide what to do with a new task?
If fewer than corePoolSize threads are running it starts a new thread. Otherwise it queues the task. Only when the queue is full does it grow towards maximumPoolSize, and if that is also full the rejection policy applies. Executors.newFixedThreadPool uses an unbounded queue, which can hide overload until memory runs out, so production code often configures a bounded queue explicitly.
How do you size a thread pool?
For CPU-bound work start near the number of cores. For work that waits on I/O, a larger pool is appropriate, roughly cores multiplied by (1 + wait time / compute time). Validate with load tests rather than trusting a formula.
Concurrent utilities
How would you implement a producer-consumer?
Use a BlockingQueue, which handles the waiting and signalling for you. put() blocks when the queue is full and take() blocks when it is empty.
BlockingQueue<Task> queue = new ArrayBlockingQueue<>(100);
// producer
queue.put(task);
// consumer
Task t = queue.take();
process(t);What is a safe way to write a singleton?
An enum singleton, or the initialisation-on-demand holder idiom, relies on class initialisation, which the JVM guarantees is thread-safe. If you use double-checked locking, the instance field must be volatile.
How do atomic classes work?
AtomicInteger and friends use compare-and-set (CAS) hardware instructions: read the current value, compute the new one, and set it only if the value has not changed, retrying otherwise. This avoids locks for simple counters and flags.
How does CompletableFuture help?
It lets you chain asynchronous steps (thenApply, thenCompose), combine results (thenCombine, allOf) and handle errors (exceptionally, handle) without blocking a thread on get(). Pass your own Executor so work does not all land on the common pool.
Practice this on Quick Tech Learners
Reading answers is only the first step. Quick Tech Learners lets you turn concurrency concepts into active practice once you create an account and sign in.
- Interview Insights: browse interview questions shared by the community and filter them by topic tag.
- Practice Quiz: choose the concepts you want to revise, answer questions, and review how you did.
- Coding Practice: write and run code in a browser editor against test cases. Pick a language, a difficulty band from Beginner to Expert, and either Practice mode (with hints) or Interview mode.
- Study Plan and reports: set the topics you are preparing, then check performance, mastery and readiness reports to see where the gaps are.
- Share Interview Question: add a question you were asked so other learners can practise it.