Skip to content

Instantly share code, notes, and snippets.

@aerodame
Created February 1, 2025 23:33
Show Gist options
  • Save aerodame/597723949002d0611eab70fab0f2b7ab to your computer and use it in GitHub Desktop.
Save aerodame/597723949002d0611eab70fab0f2b7ab to your computer and use it in GitHub Desktop.
class SimpleSemaphore {
private int S;
public SimpleSemaphore(int initialPermits) {
this.S = initialPermits;
}
public synchronized void acquire() throws InterruptedException {
while (S <= 0) {
wait(); // Block until a permit is available
}
S--;
}
public synchronized void release() {
S++;
notify(); // Wake up a waiting thread
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment