☕ Java
Intermediate
What is the Semaphore class in Java?
Answer
A Semaphore maintains a set of permits. acquire() takes a permit (blocking if none available) and release() returns a permit. It controls the number of threads that can access a resource simultaneously. A semaphore with one permit acts as a mutex. A semaphore with N permits allows up to N threads to access a resource concurrently — useful for connection pool management, rate limiting, or controlling access to a fixed number of resources. Unlike synchronized, semaphores are not tied to a specific thread — any thread can call release(), even one that did not call acquire(), enabling flexible signaling patterns.
More Java Questions
View all →- Intermediate What is the Java Collections Framework?
- Intermediate What is the difference between ArrayList and LinkedList?
- Intermediate What is HashMap in Java and how does it work internally?
- Intermediate What is the difference between HashMap and HashTable in Java?
- Intermediate What is the difference between HashMap and LinkedHashMap?