From the course: Parallel and Concurrent Programming with Java 2

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Semaphore

Semaphore - Java Tutorial

From the course: Parallel and Concurrent Programming with Java 2

Start my 1-month free trial

Semaphore

- [Baron] A semaphore is another synchronization mechanism that can be used to control access to shared resources, sort of like a mutex, but unlike a mutex, a semaphore can allow multiple threads to access the resource at the same time, and it includes a counter to track how many times it's been acquired or released. As long as the semaphore's count value is positive, any thread can acquire the semaphore, which then decrements that counter value. If the counter reaches zero, then threads trying to acquire the semaphore will be blocked and placed in a queue to wait until it's available. When a thread is done using the resource, it releases the semaphore, which increments that counter value and if there are any other threads waiting to acquire the semaphore, they'll be signaled to wake up and do so. - Hmm, looks like my phone's about to die. - Lucky us. There is a charger right here. - Nice, this charger has two ports. So it can be used by up to two devices at the same time. You can…

Contents