Skip to content

Files

Latest commit

ac721c6 · Jun 30, 2017

History

History
This branch is 1 commit ahead of, 2406 commits behind iluwatar/java-design-patterns:master.

semaphore

layout title folder permalink pumlid categories tags
pattern
Semaphore
semaphore
/patterns/semaphore/
HSV14SCm20J0Lk82BFxf1ikCfOn06ZZizfDVVhjRjphobFJnQi2ADv7pKwwEbaU6U9q6CPGwbVh8Xy5E7xvvFoNwPVjYGDo2bEC72b5URRgGeFvNqhMirF45
Concurrency
Java
Difficulty-Intermediate

Also known as

Counting Semaphore

Intent

Create a lock which mediates access to a pool of resources. Only a limited number of threads, specified at the creation of the semaphore, can access the resources at any given time. A semaphore which only allows one concurrent access to a resource is called a binary semaphore.

alt text

Applicability

Use a Semaphore when

  • you have a pool of resources to allocate to different threads
  • concurrent access to a resource could lead to a race condition

Credits