Semaphores in Java – Part I
Semaphores can be used for mutual exclusion and thread synchronization. Instead of busy waiting and wasting CPU cycles, a thread can block on a semaphore (the operating system removes the thread from the CPU scheduling or “ready” queue) if it must wait to enter its critical section or if the resource it wants is not available.
Mutual exclusion pseudocode:
semaphore S = 1; … P(S); N=N+1; V(S);
Condition synchronization pseudocode (resource availability):
semaphore tapeDrives = 7; … P(tapeDrives); useTapeDrive(); V(tapeDrives);
Java has implicit binary semaphores of the form
Object mutex = new Object();
/*...*/
synchronized (mutex) {
/*...*/
}
that can be used for mutual exclusion. Only one thread at a time can be executing inside the synchronized block.
Java synchronization primitives built in Java (monitors and wait sets) behind synchronized, wait(), and notify(). Fortunately, Java allows you to implement all the familiar synchronization schemes on top of monitors and wait sets.