Counter exceeds max limit
Review this Java rate-limiter counter that uses atomics.
Does the use of AtomicInteger make this thread-safe?
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 25 min
Mark a line and say what kind of problem it is.0 findings
1class Limiter {
2 private final AtomicInteger count = new AtomicInteger(0);
3 private final int max;
4 Limiter(int max) { this.max = max; }
5
6 boolean tryAcquire() {
7 if (count.get() < max) { // (1) read
8 count.incrementAndGet(); // (2) read-modify-write
9 return true;
10 }
11 return false;
12 }
13 void release() { count.decrementAndGet(); }
14}
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.