Notify wakes one waiter only
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.
0:00 of about 40 min
Mark a line and say what kind of problem it is.0 findings
1class Buffer {
2 private final Queue<Integer> q = new ArrayDeque<>();
3 private final int cap;
4 Buffer(int cap) { this.cap = cap; }
5
6 void put(int x) throws InterruptedException {
7 synchronized (q) {
8 if (q.size() == cap) { // (1)
9 q.wait();
10 }
11 q.add(x);
12 q.notify(); // (2)
13 }
14 }
15 int take() throws InterruptedException {
16 synchronized (q) {
17 if (q.isEmpty()) { q.wait(); }
18 int x = q.remove();
19 q.notify();
20 return x;
21 }
22 }
23}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.