Code Room
Code reviewHard
Question
Review this Java bounded-buffer producer using wait/notify.
There are two concurrency bugs — name both.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.
Learn the concepts
class Buffer { private final Queue<Integer> q = new ArrayDeque<>(); private final int cap; Buffer(int cap) { this.cap = cap; } void put(int x) throws InterruptedException { synchronized (q) { if (q.size() == cap) { // (1) q.wait(); } q.add(x); q.notify(); // (2) } } int take() throws InterruptedException { synchronized (q) { if (q.isEmpty()) { q.wait(); } int x = q.remove(); q.notify(); return x; } }}Run or narrate your approach, then ask the coach.