Java Concurrency Interview

The questions you will actually be asked, answered the way you would say them out loud: the mechanism, the trade-off, and the follow-up that comes next.

Threads and lifecycle

The warm-up round. Most interviews open here, and most candidates lose points not because they cannot name the thread states but because they cannot say what the JVM actually does at each transition.

  1. What is the difference between a process and a thread, and what does that cost you in Java? junior
  2. Why is Thread.stop() deprecated, and what replaced it? junior
  3. What actually happens when you call interrupt() on a thread? mid
  4. Why is swallowing InterruptedException a bug, and what is the correct handling? mid
  5. What is the difference between a daemon thread and a user thread? junior
  6. What is the difference between Runnable and Callable, and where does Future fit? junior
  7. Why must you call start() rather than run()? junior
  8. What is the difference between BLOCKED, WAITING and TIMED_WAITING in a thread dump? mid
  9. Does Thread.yield() do anything you can rely on? mid
  10. Two threads, one Thread.sleep(1000) inside a synchronized block. What does the other thread see? junior
  11. Can a thread be garbage collected while it is running? senior
  12. What happens when a thread throws an exception nobody catches? junior

The Java memory model

The rules every other topic depends on. An answer here that says "volatile makes it thread safe" ends the interview early; an answer that can name the happens-before edge passes almost any follow-up.

  1. What is happens-before, in one sentence, and why does it matter? mid
  2. Which happens-before edges do you get for free? mid
  3. What does volatile guarantee, and what does it not? junior
  4. Why does double-checked locking need the field to be volatile? senior
  5. What is safe publication, and which ways of achieving it can you name? senior
  6. What does final guarantee, and what breaks that guarantee? senior
  7. What is instruction reordering, and can you observe it from ordinary Java? senior
  8. Where do memory barriers come into this, and which ones does the JVM emit? senior
  9. Is long and double assignment atomic in Java? mid
  10. Two threads increment a shared int a million times each with no synchronisation. What is the final value and why? junior
  11. What is the difference between a data race and a race condition? senior
  12. What does the Java memory model promise a correctly synchronized program? senior

synchronized, monitors, wait and notify

The oldest tool in the language, and still the one most production code uses. The questions here separate people who have written synchronized from people who know what the JVM does with it.

  1. What exactly does synchronized lock? junior
  2. Why is locking on a String literal or a boxed Integer a bug? mid
  3. What is reentrancy, and what would break without it? mid
  4. Why must wait() be called inside a loop? mid
  5. When should you prefer notifyAll() over notify()? mid
  6. Why does wait() throw IllegalMonitorStateException sometimes? junior
  7. What is the difference between synchronized and ReentrantLock in practice? mid
  8. Is an uncontended synchronized block expensive? senior
  9. Can you synchronized on the same object from two methods and still have a race? mid
  10. What is lock striping, and when does it stop helping? senior

Locks, conditions and AQS

Everything in java.util.concurrent.locks is one class in a trench coat: AbstractQueuedSynchronizer. Knowing that turns a dozen separate facts into one mechanism you can reason about.

  1. What is AbstractQueuedSynchronizer and which classes are built on it? senior
  2. What does a fair lock actually do, and what does it cost? mid
  3. When is ReentrantReadWriteLock a win, and when is it a trap? senior
  4. What is optimistic reading in StampedLock? senior
  5. What does Condition give you that wait/notify does not? mid
  6. How do you implement a timeout on acquiring a lock, and why would you? mid
  7. What is LockSupport.park, and why is it better than suspend/resume? senior
  8. What is a spin lock, and when is spinning better than blocking? senior
  9. Can a thread deadlock against itself? mid
  10. Why is lock() outside try and unlock() inside finally the required shape? junior

Synchronizers: latches, barriers, semaphores

The coordination primitives, and the topic with the single most predictable interview question in Java: the difference between a latch and a barrier. Knowing the whole family is what separates a memorised answer from a chosen one.

  1. What is the difference between CountDownLatch and CyclicBarrier? mid
  2. When would you use a latch as a starting gate rather than as a finish line? mid
  3. What does a Semaphore actually protect? junior
  4. Why is Semaphore the right way to limit concurrency with virtual threads? senior
  5. What is a Phaser and when does it beat a CyclicBarrier? senior
  6. What is an Exchanger for? senior
  7. How does CompletableFuture.allOf compare to a CountDownLatch for "wait for N tasks"? mid
  8. Why must a Semaphore release live in a finally, and what goes wrong if it does not? mid
  9. Is CountDownLatch.await() a happens-before edge? senior
  10. Which synchroniser would you choose, and how do you decide? mid

Atomics and compare-and-swap

Lock-free code is not magic, it is one CPU instruction plus a retry loop. Interviewers use this topic to find out whether "lock-free" is a word you have read or a mechanism you can draw.

  1. What is compare-and-swap, and how does AtomicInteger.incrementAndGet use it? mid
  2. What is the ABA problem, and how do you avoid it? senior
  3. Why is LongAdder faster than AtomicLong, and when is it not? senior
  4. What is false sharing, and how do you fix it? senior
  5. What do getAndUpdate, updateAndGet and accumulateAndGet do, and what is the catch? mid
  6. How would you write a thread-safe non-blocking counter without atomics? senior
  7. What is the difference between compareAndSet and weakCompareAndSet? senior
  8. Is AtomicReference enough to make the object it points at thread safe? mid
  9. What does "lock-free" mean, and how does it differ from "wait-free"? senior
  10. Can you replace volatile boolean running with AtomicBoolean, and should you? junior
  11. Does declaring an array volatile make its elements volatile? mid

Concurrent collections

Choosing the wrong collection is the most common real-world concurrency bug, because it does not crash. It just loses data occasionally. Interviewers ask this topic to see whether you know the difference between "thread safe" and "correct for my access pattern".

  1. Why is Hashtable or Collections.synchronizedMap worse than ConcurrentHashMap? junior
  2. How does ConcurrentHashMap work since Java 8? senior
  3. Why does ConcurrentHashMap forbid null keys and values? mid
  4. What is the iteration behaviour of a concurrent collection, and what is ConcurrentModificationException? mid
  5. When is CopyOnWriteArrayList the right choice? mid
  6. ArrayBlockingQueue or LinkedBlockingQueue, and does bounded matter? mid
  7. What is ConcurrentSkipListMap for, and how does it differ from ConcurrentHashMap? mid
  8. How would you build a thread-safe cache with computeIfAbsent, and what is the trap? senior
  9. What does Collections.unmodifiableList guarantee about thread safety? mid
  10. Is ConcurrentHashMap.size() reliable, and what should you use instead? mid
  11. When is a plain HashMap behind your own lock better than ConcurrentHashMap? senior
  12. Many threads count words into one map. How do you make the counting correct? junior
  13. What is the difference between add, offer and put on a BlockingQueue? junior

Executors and thread pools

The topic where interview answers most often come from a tutorial rather than from an incident. Every question below has cost somebody a production outage at some point.

  1. Walk through what ThreadPoolExecutor does with a submitted task. mid
  2. Why is Executors.newFixedThreadPool considered dangerous in production? mid
  3. How do you size a thread pool? mid
  4. What is the difference between shutdown() and shutdownNow(), and how do you shut down properly? mid
  5. What happens to an exception thrown inside a pooled task? mid
  6. What happens when a ScheduledExecutorService task throws? senior
  7. Which rejection policy would you choose and why? senior
  8. What is invokeAll versus invokeAny, and where does CompletionService fit? mid
  9. Why name your threads, and how? junior
  10. Why must a ThreadLocal be removed in a pooled thread? senior
  11. What is work stealing, and which executors use it? senior
  12. What breaks when a pooled task submits another task to the same pool and waits for it? senior
  13. Why is ScheduledExecutorService preferred to java.util.Timer? mid

CompletableFuture and async composition

Where interviews go once "I would use a thread pool" is settled. The trap in this topic is that everything compiles and runs, and only the thread it runs on and the exception you never see are wrong.

  1. What does CompletableFuture add over Future? junior
  2. What is the difference between thenApply and thenCompose? mid
  3. Which thread runs your callback if you use thenApply rather than thenApplyAsync? senior
  4. Why should you pass an explicit executor to the *Async methods? mid
  5. How do exceptions propagate through a chain, and what do exceptionally, handle and whenComplete do? mid
  6. What does allOf return, and how do you collect the results? mid
  7. Does cancelling a CompletableFuture stop the work? senior
  8. How do you add a timeout to a CompletableFuture? mid
  9. What is the difference between join() and get()? junior
  10. When is CompletableFuture the wrong tool now that virtual threads exist? senior

Fork/join and parallel streams

One method call turns a stream parallel, which is why this topic is full of code that is slower than the sequential version it replaced. Interviewers ask it to see whether you can say no.

  1. How does ForkJoinPool differ from a fixed thread pool? mid
  2. What is the correct shape of a RecursiveTask? mid
  3. What is the common pool, and why does it cause trouble? senior
  4. When is a parallel stream faster, and when is it slower? mid
  5. Why is forEach on a parallel stream dangerous, and what should you use? mid
  6. What does Collectors.toConcurrentMap change, and when is groupingByConcurrent actually used? senior
  7. What is a Spliterator and why does it matter for parallelism? senior
  8. Can you run a parallel stream in your own pool, and should you? senior
  9. Why is Stream.iterate a poor source for a parallel stream? mid
  10. What happens if a fork/join task blocks on I/O? senior
  11. What does parallelStream() change compared with stream()? junior

Virtual threads and structured concurrency

Java 21 made threads cheap. That invalidates a decade of advice, and interviewers now use this topic to find out whether a candidate has actually used them or only read the announcement.

  1. What is a virtual thread, and how is it scheduled? mid
  2. What is pinning, and what causes it? senior
  3. Should you pool virtual threads? mid
  4. When do virtual threads not help? senior
  5. What problem does structured concurrency solve? mid
  6. What are ShutdownOnFailure and ShutdownOnSuccess for? senior
  7. What is a ScopedValue, and why is it preferred to ThreadLocal here? senior
  8. How do you debug a system running a million virtual threads? senior
  9. Do virtual threads change how you use synchronized in library code? senior
  10. Is Thread.sleep still a bad idea on a virtual thread? mid
  11. What does Executors.newVirtualThreadPerTaskExecutor().close() do? mid
  12. Is a virtual thread faster than a platform thread? junior

Concurrency in real systems

Where core Java meets the frameworks people actually ship. These questions are the ones a backend interview reaches after the language questions are settled, and the answers are usually about which thread your code is on and which pool is about to run out.

  1. Which thread runs a Spring @Async method, and what does it lose? mid
  2. Why does @Transactional not work across threads? mid
  3. Your service has 200 HTTP threads and a pool of 10 database connections. Where is the bottleneck? senior
  4. Is a Spring singleton bean thread safe? junior
  5. What is the servlet thread model, and what does async servlet processing change? senior
  6. What does a reactive framework buy you over threads, and what does it cost? senior
  7. How do you propagate request context (trace id, user) across threads? mid
  8. HttpClient, RestTemplate or WebClient: what concurrency questions do you ask before choosing? mid
  9. What breaks when you use a parallel stream inside a web request? senior
  10. How does a connection pool interact with your thread pool, and what should you monitor? senior
  11. Why can one slow @Scheduled job delay every other scheduled job in a Spring app? mid
  12. KafkaConsumer is not thread safe. How do you process its records in parallel? senior

Deadlock, livelock and diagnostics

The half of concurrency that only exists in production. Interviewers like this topic because it cannot be answered from a tutorial: either you have read a thread dump at 3am or you have not.

  1. What four conditions must hold for a deadlock, and which is easiest to break? mid
  2. Show the classic transfer deadlock and fix it. mid
  3. How do you diagnose a deadlock in a running JVM? mid
  4. What is a livelock, and how does it differ from a deadlock? mid
  5. What is starvation, and what causes it in a JVM? mid
  6. How do you read a thread dump? What do you look for first? senior
  7. Your service stops responding but CPU is at zero. What is your first hypothesis? senior
  8. Which JDK tools do you use for concurrency problems? mid
  9. Why does a debugger so often make a concurrency bug disappear? mid
  10. A colleague reports a bug that happens once a week in production and never locally. How do you approach it? senior
  11. What is a deadlock, and why does it not resolve itself? junior

Patterns and antipatterns

Design judgement, which is what a senior interview is actually testing. The right answer here is usually the one that removes shared mutable state rather than the one that guards it more cleverly.

  1. What does thread confinement mean, and what are the three kinds? mid
  2. Why is immutability the strongest thread safety guarantee? junior
  3. What is the producer/consumer pattern and what does the queue actually provide? junior
  4. What is the balking pattern, and where would you use it? mid
  5. What is the double-checked locking antipattern really about? mid
  6. Why is "synchronize everything" a bad strategy? mid
  7. What is the thread-per-task versus task-per-thread distinction, and why did pools exist? mid
  8. Why should you not start a thread from a constructor? senior
  9. When would you choose an object pool, and when is it the wrong answer? senior
  10. What is the two-phase termination pattern? mid
  11. How do you make a class document its own thread safety? mid
  12. What are the thread-safe ways to write a singleton, and which would you pick? mid

Testing concurrent code

Asked far less often than it should be, and a strong differentiator when you can answer it. Most candidates describe a test with a Thread.sleep in it, which is exactly the thing that does not work.

  1. Why is Thread.sleep in a test a bug rather than a delay? mid
  2. How do you write a test that actually creates contention? mid
  3. Why can a unit test not reliably catch a visibility or reordering bug? senior
  4. What is jcstress and when do you reach for it? senior
  5. How do you test that something is not possible, such as a deadlock? senior
  6. What makes a concurrency test flaky, and how do you deal with flakiness? mid
  7. How would you benchmark two concurrent implementations honestly? senior
  8. Can you unit test code that uses the current time or a scheduler? mid
  9. What static analysis helps with concurrency? mid
  10. What would you add to a code review checklist for concurrent code? senior
  11. What is the difference between assertTimeout and assertTimeoutPreemptively in JUnit 5? junior

Live coding puzzles

The whiteboard round. Each of these has a canonical solution short enough to write in ten minutes, and an obvious wrong version the interviewer is hoping you avoid.

  1. Print numbers 1 to 100 with two threads, one printing odd and one even, in order. mid
  2. Implement a bounded blocking queue with wait and notify. mid
  3. Make three tasks run concurrently but print their results in a fixed order. mid
  4. Implement a rate limiter that allows N operations per second. senior
  5. Solve the dining philosophers problem without deadlock. mid
  6. Write a thread-safe lazily initialised cache where each key is computed once. senior
  7. Implement a simple CountDownLatch yourself. senior
  8. Write a non-blocking stack. senior
  9. Run a task exactly once no matter how many threads call it. mid
  10. Two threads, one increments and one prints a shared counter. What could go wrong, and how do you fix it by hand? junior