[Java - Synchronization] Semaphores and Mutex

1) Semaphore

Basically, a counter that limits the number of concurrent threads accessing a specific resource.

In general, to use a semaphore, the thread that wants access to the shared resource tries to acquire a permit.
  • If the semaphore’s count is greater than zero, then the thread acquires a permit, which causes the semaphore’s count to be decremented.
  • Otherwise, the thread will be blocked until a permit can be acquired.
  • When the thread no longer needs an access to the shared resource, it releases the permit, which causes the semaphore’s count to be incremented.
  • If there is another thread waiting for a permit, then that thread will acquire a permit at that time.

Java provide Semaphore class in java.util.concurrent package that implements this mechanism, so you don’t have to implement your own semaphores.




1.1) Constructor

Semaphore(int permits): creates Semaphore with given number of permits and nonfair fairness setting.
Semaphore(int permits, boolean fair): creates Semaphore with given number of permits and given fairness setting.

1.2) Example



1.3) Ouptut

Total available Semaphore permits : 2
A : acquiring lock...
B : acquiring lock...
B : available Semaphore permits now: 2
D : acquiring lock...
C : acquiring lock...
A : available Semaphore permits now: 2
C : available Semaphore permits now: 1
D : available Semaphore permits now: 1
B : got the permit!
B : is performing operation 1, available Semaphore permits : 0
A : got the permit!
A : is performing operation 1, available Semaphore permits : 0
A : is performing operation 2, available Semaphore permits : 0
B : is performing operation 2, available Semaphore permits : 0
A : is performing operation 3, available Semaphore permits : 0
B : is performing operation 3, available Semaphore permits : 0
B : is performing operation 4, available Semaphore permits : 0
A : is performing operation 4, available Semaphore permits : 0
B : is performing operation 5, available Semaphore permits : 0
A : is performing operation 5, available Semaphore permits : 0
A : releasing lock...
B : releasing lock...
C : got the permit!
D : got the permit! 

A : available Semaphore permits now: 1
D : is performing operation 1, available Semaphore permits : 0
C : is performing operation 1, available Semaphore permits : 0
B : available Semaphore permits now: 1
C : is performing operation 2, available Semaphore permits : 0
D : is performing operation 2, available Semaphore permits : 0
C : is performing operation 3, available Semaphore permits : 0
D : is performing operation 3, available Semaphore permits : 0
D : is performing operation 4, available Semaphore permits : 0
C : is performing operation 4, available Semaphore permits : 0
C : is performing operation 5, available Semaphore permits : 0
D : is performing operation 5, available Semaphore permits : 0
C : releasing lock...
C : available Semaphore permits now: 1
D : releasing lock...
D : available Semaphore permits now: 2

1.4) Explanation

As you can see the output, the bolded part notes the part where the locks are released and thread C and D can get the permit.

1.5) Timed Semaphore

TimedSemaphore allows a number of permits as a simple Semaphore but in a given period of time, after this period the time reset and all permits are released.

2) Mutex

Mutex is the Semaphore with an access count of 1; only one thread to access a resource at once.
Example: when a client is accessing a file, no one else should have access the same file at the same time.

2.1) Implementation

Semaphore mutex = new Semaphore(1);

Resources

Comments

Popular posts from this blog

[Redis] Redis Cluster vs Redis Sentinel

[Unit Testing] Test Doubles (Stubs, Mocks....etc)

[Node.js] Pending HTTP requests lead to unresponsive nodeJS