A Java implementation of the classic second readers-writers problem, using semaphores to allow concurrent read access while giving writers priority (preventing writer starvation).
S— binary semaphore guarding the shared resource.mutex— binary semaphore protecting the reader count.serviceQueue— a fair (FIFO) binary semaphore ("turnstile") that readers must also pass through before acquiringmutex— this is what prevents writers from starving under a steady stream of readers.readCount— tracks how many readers currently hold the resource; only the first reader acquiresS, and only the last reader releases it.
readLock()/readUnLock()— acquire/release read accesswriteLock()/writeUnLock()— acquire/release exclusive write access
Java (java.util.concurrent.Semaphore)