mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Co-authored-by: Raúl Pardo <raul.pardo@protonmail.com> Co-authored-by: SimonJorgensenMancofi <simon.jorgensen@mancofi.dk> Co-authored-by: Bjørnar Haugstad Jåtten <bjornjaat@hotmail.com>
31 lines
520 B
Java
31 lines
520 B
Java
package examples;
|
|
|
|
@ThreadSafe
|
|
public class FlawedSemaphore {
|
|
private final int capacity;
|
|
private int state;
|
|
|
|
public FlawedSemaphore(int c) {
|
|
capacity = c;
|
|
state = 0;
|
|
}
|
|
|
|
public void acquire() {
|
|
try {
|
|
while (state == capacity) {
|
|
this.wait();
|
|
}
|
|
state++; // $ Alert
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void release() {
|
|
synchronized (this) {
|
|
state--; // State can become negative
|
|
this.notifyAll();
|
|
}
|
|
}
|
|
}
|