Quick revision
The short answer
Yes, I would ban raw wait/notify in application code, with a short documented list of exceptions, because java.util.concurrent covers almost every need and these primitives are easy to misuse and hard to review. For the existing classes, I would rank them by incident history and criticality, replace the riskiest first with BlockingQueue, CountDownLatch, Semaphore or CompletableFuture, add stress tests around each component before and after the change, and enforce the rule with static analysis that flags Object.wait and notify outside approved modules.
What to remember
In this answer, we decided to ban raw wait/notify in application code with documented exceptions, because hand-written coordination is easy to misuse, hard to review, and covered by java.util.concurrent. We planned the cleanup by incident history and criticality, replaced each class behind its existing interface with a library class, protected every change with stress tests run before and after, enforced the rule with static analysis and a shrinking baseline, and explained the reasoning to the team with real incidents and a clear mapping to replacements.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Yes, I would ban raw wait/notify in application code, with a short documented list of exceptions, because java.util.concurrent covers almost every need and these primitives are easy to misuse and hard to review. For the existing classes, I would rank them by incident history and criticality, replace the riskiest first with BlockingQueue, CountDownLatch, Semaphore or CompletableFuture, add stress tests around each component before and after the change, and enforce the rule with static analysis that flags Object.wait and notify outside approved modules.
Why this question is asked
Coordination bugs are some of the most expensive bugs a team can have. They appear once a week under load, vanish when someone attaches a debugger, and leave nothing in the logs except a thread dump full of WAITING threads. When a codebase has dozens of hand-written wait/notify classes, each one is a small, unreviewed concurrency library. Interviewers ask this question to see whether a senior engineer can turn a technical judgement into a policy that people accept, and whether they can clean up legacy risk without causing new outages. In this answer, we will look at why these classes fail, decide on the rule and its exceptions, plan the migration of existing code, and then see how to enforce the rule and explain it to the team.
1Why Hand-Written wait/notify Code Stalls
wait() and notifyAll() are the low-level tools behind guarded blocks: a thread waits on an object's monitor until another thread changes some state and notifies it. Used correctly they are fine. The trouble is how many details must be correct at the same time, and how little the code shows when one of them is wrong.
The common failures are well known. An if instead of a while lets a thread continue after a spurious wakeup. notify() instead of notifyAll() wakes the wrong waiter, and the notification is lost while the right waiter sleeps on. State changed outside the lock creates a window where a waiter checks, the state changes, the notification fires, and only then the waiter sleeps, forever. InterruptedException caught and ignored leaves a thread that cannot be shut down. A timed wait that does not recompute the remaining time waits too long after each early wakeup.
Each of these looks reasonable in a code review, passes unit tests, and fails only under particular timing. That is the core argument for the ban: not that wait/notify is wrong, but that reviewers cannot reliably tell a correct use from a broken one.
2Deciding the Rule and Its Exceptions
A ban works when it is specific and comes with a clear replacement. The rule I would propose is: application code does not call Object.wait, notify or notifyAll; use the java.util.concurrent classes instead; exceptions are allowed in a named set of modules, such as a low-level platform library, with a written justification and review by one of the people who own concurrency reviews.
Most needs map directly onto library classes, and showing that mapping is what makes the rule feel helpful rather than arbitrary.
| Hand-written pattern | Library replacement |
|---|---|
| Wait until a flag is set once | CountDownLatch(1) or CompletableFuture |
| Producer and consumer with a list | ArrayBlockingQueue or LinkedBlockingQueue |
| Limit concurrent users of a resource | Semaphore |
| Wait for a result from another thread | CompletableFuture, Future |
| All threads meet at a point, repeatedly | CyclicBarrier or Phaser |
| Several conditions on one lock | ReentrantLock with Condition objects |
3Prioritising the Existing Classes
Rewriting dozens of classes at once would be its own risk, because each rewrite is a change to concurrent behaviour. So we treat the migration as a backlog and order it by risk.
First, we build an inventory: search for wait( and notify on Object, list each class, who calls it, and which services use it. Second, we score each class on two things. Incident history: has it appeared in a thread dump during an incident, or in a postmortem? Criticality: is it on the request path of an important service, or in a nightly batch tool? A class that caused two stalls in the payments path goes first; a class in an internal admin tool that has never failed goes last, and may simply be left alone until someone touches it.
- Inventory and scoreincidents, criticality, callers
- Write a stress testrun it against the old class
- Replace internalslibrary class, same interface
- Stress test passes?YesCanary deployNoFix and repeat
- Compare metricslatency, stuck threads, errors
- Delete the old codeand add it to the lint baseline
6 steps in order. Play them, or step through them one at a time.
Each class goes through the same steps, highest risk first.
4Testing Before and After the Change
The tests are what make the migration safe, and they must exist before the change, not just after. A stress test for a coordination class runs many threads against it for many iterations and checks an invariant: every item produced is consumed exactly once, no thread is still waiting at the end, the total of permits is unchanged. We run it with a timeout so that a stall fails the test instead of hanging the build.
Running this against the old class first does two things. It sometimes reproduces the stall that caused the incident, which proves the test is meaningful. And it gives a baseline for throughput, so we can show the replacement is not slower. The same test then runs against the new implementation and stays in the suite as a regression guard. For shared libraries, jcstress, the OpenJDK tool for concurrency stress tests, is worth using on the smallest interactions.
5Enforcing the Rule Without Blocking the Team
A rule that lives only in a wiki is forgotten in months. We enforce it in the build. ArchUnit can express it as a test, for example that no classes outside the approved packages call methods named wait, notify or notifyAll on java.lang.Object. Checkstyle, PMD or Error Prone rules can do the same at compile or lint time; check which of them your build already uses, since the exact rule names differ between tools.
On the first day, the existing violations are recorded in a baseline, so the build does not fail on old code. New violations fail the build. As each class is migrated, it leaves the baseline, and the baseline shrinking is a simple progress measure we can report.
6Explaining the Reason to the Team
Engineers accept a ban when they understand it is about reviewability, not about doubting anyone's skill. The message I would give is short: these primitives are easy to misuse in ways that tests and reviews do not catch, our incidents show it, and the standard library gives us tested versions of every pattern we use. I would share one or two of the real incidents, show the before and after code, and make the exception process easy and fast, so nobody feels the rule blocks legitimate low-level work. Pairing the announcement with a short internal guide that maps each old pattern to its replacement turns the rule into help.
7Follow-up questions
Is a ban too strong? Why not just review wait/notify code more carefully?
Careful review is exactly what has failed already: the stalls came from code that was reviewed. The problems are timing-dependent, so a reviewer must mentally simulate interleavings, and few people can do that reliably. Since the library covers nearly every case, the ban costs very little and removes a whole class of defects. The exception process keeps it from being absolute where a real need exists.
What about synchronized blocks in general, would you ban those too?
No. Simple mutual exclusion with synchronized is easy to read and review, and since Java 24 virtual threads no longer pin their carrier thread inside synchronized, which removed the main practical reason to avoid it. The risk is in hand-built coordination with wait and notify, not in short critical sections. I would still encourage small critical sections and no blocking calls inside them.
8Conclusion
In this answer, we decided to ban raw wait/notify in application code with documented exceptions, because hand-written coordination is easy to misuse, hard to review, and covered by java.util.concurrent. We planned the cleanup by incident history and criticality, replaced each class behind its existing interface with a library class, protected every change with stress tests run before and after, enforced the rule with static analysis and a shrinking baseline, and explained the reasoning to the team with real incidents and a clear mapping to replacements.
Open with
Yes, I would ban raw wait and notify in application code, with a documented exception process for a few low-level modules. My reason is not that the primitives are wrong but that they are easy to misuse and very hard to review: an if instead of a while, notify instead of notifyAll, a state change outside the lock, or a swallowed interrupt all look fine in review and only fail under timing we cannot reproduce.
Then, if they want more
Our incidents show that. And java.util.concurrent covers almost everything we need: CountDownLatch or CompletableFuture for one-time signals, BlockingQueue for producer and consumer, Semaphore for limits, and ReentrantLock with Conditions for the rare complex case.
For the existing code, I would first inventory the classes and rank them by incident history and criticality, so the class that stalled the payments path twice goes first and an untouched admin tool can wait.
For each one I would write a stress test with many threads and an invariant, run it against the old code to get a baseline, then replace the internals behind the same interface and run it again, and roll out with a canary.
To keep the rule, I would add an ArchUnit or lint check that flags Object.wait and notify outside approved packages, with a baseline of current violations that shrinks as we migrate. And I would explain it to the team using real incidents, so it reads as protection rather than distrust.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your codebase has dozens of hand-written wait/notify coordination classes written over...": https://scrivio-interview-library.netlify.app/java/synchronization/1b136bd03e/