Created
February 1, 2025 23:33
-
-
Save aerodame/597723949002d0611eab70fab0f2b7ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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