VWhat is a race condition, and how do you prevent it in Java?
Interviewer questionJava concurrency
Concurrency is where interviewers separate people who have read about threads from people who have debugged them. Expect a mix of definitions and small scenarios: what goes wrong, why it goes wrong and how you would fix it.
A good spoken answer names the problem first, shows a short example in words, and then offers one or two solutions with their cost. Always say the cost, because every solution to a concurrency problem has one.
- ✓Words → phrases → sentences → speaking
- ✓Answer meaning
- ✓Language
Common questions and sample answers
What is a race condition, and how do you prevent it in Java?
What the interviewer wants to hear. A concrete example, such as an unsynchronised counter, and more than one fix.
Sample answer to practise aloudA race condition happens when the result depends on the timing of threads. A classic example is a counter: two threads read the same value, both add one, and both write it back, so one update is lost. To prevent it, I can make the operation atomic with AtomicInteger, protect the critical section with synchronized or a ReentrantLock, or avoid shared mutable state completely by using immutable objects or keeping data inside one thread. I prefer the last option when possible, because it removes the problem instead of managing it.
What is the difference between volatile and synchronized?
What the interviewer wants to hear. Visibility versus atomicity and mutual exclusion.
Sample answer to practise aloudVolatile guarantees visibility. When one thread writes a volatile variable, other threads see the new value. But volatile does not make a compound action atomic. For example, count plus plus on a volatile integer is still a race. Synchronized gives mutual exclusion, so only one thread enters the block at a time, and it also guarantees visibility when the lock is released and acquired. I use volatile for simple flags, like a stop signal, and synchronized or atomic classes when I update state in several steps.
What is a deadlock, and how do you avoid it?
What the interviewer wants to hear. A clear picture of two threads waiting for each other, and practical prevention.
Sample answer to practise aloudA deadlock happens when two or more threads wait for each other's locks forever. Thread A holds lock one and wants lock two, while thread B holds lock two and wants lock one. To avoid it, I always acquire locks in the same global order. I keep critical sections short and I do not call unknown code while holding a lock. Another option is tryLock with a timeout, so a thread can give up and retry. If I suspect a deadlock in production, I take a thread dump, and the JVM usually shows the cycle clearly.
Why use a thread pool instead of creating threads manually?
What the interviewer wants to hear. The cost of threads, bounded resources, and one real configuration risk.
Sample answer to practise aloudCreating a thread is expensive, and unlimited threads can exhaust memory and slow everything down. A thread pool reuses a fixed number of threads and puts extra tasks in a queue, which gives us control over the load. I use ExecutorService, and I prefer to configure ThreadPoolExecutor directly, because some factory methods use an unbounded queue, and that can hide a memory problem. For blocking input and output in Java 21, I would also consider virtual threads, because they are cheap and make the code simpler.
How is ConcurrentHashMap different from a synchronized map?
What the interviewer wants to hear. How the locking works, and why compound operations matter.
Sample answer to practise aloudA map wrapped with Collections.synchronizedMap uses one lock for the whole map, so threads wait for each other even when they touch different keys. ConcurrentHashMap allows many threads to read and write at the same time, with very fine-grained locking and compare-and-swap operations inside. It also has atomic methods like putIfAbsent, compute and merge. These are important, because a check followed by a put is not atomic on any map. One difference to remember is that ConcurrentHashMap does not allow null keys or null values.
How to say the key terms
- thread
- thred (the "th" is soft, like in "think")
- mutex
- MYOO-teks
- deadlock
- DED-lok
- atomic
- uh-TOM-ik
- volatile
- VOL-uh-tul (US) or VOL-uh-tyle (UK)
- asynchronous
- ay-SIN-kruh-nus
Useful phrases for answering
- The problem is that two threads can…
- To make this thread-safe, I would…
- The cost of that approach is…
- In a real project, I would measure first.
- Let me walk you through a scenario.
- I would take a thread dump to see where the threads are blocked.
Common mistakes to avoid
- Mixing up concurrency and parallelism. Concurrency means dealing with many tasks at once; parallelism means running them at the same moment on several cores. One sentence is enough.
- Saying "just use synchronized everywhere". It shows you cannot judge cost. Mention contention and the size of the locked section.
- Describing the problem only in theory. A two-sentence example, such as a shared counter, is easier to follow and easier to remember.
- Forgetting to say how you would find the bug. Mention thread dumps, logs with thread names and stress tests.
Frequently asked questions
Do I need to know the Java memory model in detail?
You should understand the idea of happens-before and why visibility matters, and be able to explain it simply. The exact rules of reordering are usually needed only for senior roles.
Are virtual threads a common interview topic now?
More and more, if the team uses Java 21 or newer. Be able to say what they are for (many blocking tasks) and what they do not fix (CPU-heavy work and shared-state problems).
How do I explain a concurrency bug I fixed?
Use a short story: what the symptom was, how you found the cause, what you changed and how you proved it was fixed. Interviewers often like the debugging path more than the final code.
Reading an answer is not the same as saying it. Practise each question out loud, then compare the meaning and the language of your own answer.
Try SpeakRole ↗Other topics in this area