In the previous examples in CountDownlatch and CyclicBarrier we saw how threads communicate with each other and wait till other completes.
Here Semaphore class in concurrent package is used as a pool that can be acuired and released. very much like lock but with a difference. When a thread acquires a lock not other thread can enter the synchronise block.
That means that the pool size of resource is always 1. Here in Semaphore we can define the poolsize and threads can acuire locks till there is resource left in the pool. Lets see how it works.
Lets continue the example from previous chapters.
The organization needs to recruit 4 Java developers. HR Manager asks 4 Tech Leads to conduct test and recruit the canditates. The problem here is that the Tech Leads have only 2 test paper and the photo copy machine is down. That means at a time only to candidates can give the test and other 2 have to wait.
Lets simulate this by Semaphore example.
The above class represent Tech Lead. The Tech Lead here needs test paper to conduct the test. For this it calls questionPaperPool.acquire() if any test paper is available it will acquire the test paper and conduct the test and only release the test paper when test is completed by calling questionPaperPool.release().
Other Tech Leads will wait on calling questionPaperPool.acquire() if no test papers are available.
Lets run the program and see what the ouput is
From the output we can see that First John acquired the test paper and conducts test, while John is conducting the test Mark has also acquired another paper and is conducting test.
Both have not yet finished the test and they have not given the test paper back for others to use. That is the reason Doe and Albert are waiting for the papers to be released.
After the test is completed by wither John it releases the paper which is now acquired by Doe and Doe now conducts the test. After some time Mark also finishes the paper and gives the paper back which is now acquired by Albert and used.
So at a time only two Team Lead's could conduct the test since only two test papers were available.
Important Point
This is creating a instance of Semaphore class with pool size 2
This method call acquires a permit from the semaphore. The current thread will go in blocking state if all permits are acquired.
This method call release the acuired permit.
Thread class >> Runnable Interface >> Callable Interface >> CountDownLatch >> CyclicBarrier >> Semaphore >> Reentrant Lock >> Reentrant Lock with Condition
Here Semaphore class in concurrent package is used as a pool that can be acuired and released. very much like lock but with a difference. When a thread acquires a lock not other thread can enter the synchronise block.
That means that the pool size of resource is always 1. Here in Semaphore we can define the poolsize and threads can acuire locks till there is resource left in the pool. Lets see how it works.
Lets continue the example from previous chapters.
The organization needs to recruit 4 Java developers. HR Manager asks 4 Tech Leads to conduct test and recruit the canditates. The problem here is that the Tech Leads have only 2 test paper and the photo copy machine is down. That means at a time only to candidates can give the test and other 2 have to wait.
Lets simulate this by Semaphore example.
import java.util.concurrent.Semaphore; public class HRManager { public static void main(String args[]){ Semaphore questionPaperPool = new Semaphore(2); TechLead techLead1 = new TechLead(questionPaperPool,"John TL"); TechLead techLead2 = new TechLead(questionPaperPool,"Doe TL"); TechLead techLead3 = new TechLead(questionPaperPool,"Mark TL"); TechLead techLead4 = new TechLead(questionPaperPool,"Albert TL"); techLead1.start(); techLead2.start(); techLead3.start(); techLead4.start(); System.out.println("No work for HR manager"); } }The HR Manager above creates 4 Tech Leads and quits.
import java.util.concurrent.Semaphore; public class TechLead extends Thread { Semaphore questionPaperPool; public TechLead(Semaphore questionPaperPool, String name) { super(name); this.questionPaperPool = questionPaperPool; } @Override public void run() { try { System.out.println(Thread.currentThread().getName()+" Waiting for test question paper"); //Acquiring one question paper questionPaperPool.acquire(); System.out.println(Thread.currentThread().getName()+" acquired test paper"); System.out.println(Thread.currentThread().getName()+" Conducting test"); Thread.sleep(3000); System.out.println(Thread.currentThread().getName()+" Test done giving back the paper"); //Giving back the acquired question paper questionPaperPool.release(); } catch (InterruptedException e) { e.printStackTrace(); } } }
The above class represent Tech Lead. The Tech Lead here needs test paper to conduct the test. For this it calls questionPaperPool.acquire() if any test paper is available it will acquire the test paper and conduct the test and only release the test paper when test is completed by calling questionPaperPool.release().
Other Tech Leads will wait on calling questionPaperPool.acquire() if no test papers are available.
Lets run the program and see what the ouput is
No work for HR manager John TL Waiting for test question paper John TL acquired test paper John TL Conducting test Mark TL Waiting for test question paper Mark TL acquired test paper Mark TL Conducting test Doe TL Waiting for test question paper Albert TL Waiting for test question paper John TL Test done giving back the paper Doe TL acquired test paper Doe TL Conducting test Mark TL Test done giving back the paper Albert TL acquired test paper Albert TL Conducting test Doe TL Test done giving back the paper Albert TL Test done giving back the paper
From the output we can see that First John acquired the test paper and conducts test, while John is conducting the test Mark has also acquired another paper and is conducting test.
Both have not yet finished the test and they have not given the test paper back for others to use. That is the reason Doe and Albert are waiting for the papers to be released.
After the test is completed by wither John it releases the paper which is now acquired by Doe and Doe now conducts the test. After some time Mark also finishes the paper and gives the paper back which is now acquired by Albert and used.
So at a time only two Team Lead's could conduct the test since only two test papers were available.
Important Point
Semaphore questionPaperPool = new Semaphore(2);
This is creating a instance of Semaphore class with pool size 2
questionPaperPool.acquire();
This method call acquires a permit from the semaphore. The current thread will go in blocking state if all permits are acquired.
questionPaperPool.release();
This method call release the acuired permit.
Thread class >> Runnable Interface >> Callable Interface >> CountDownLatch >> CyclicBarrier >> Semaphore >> Reentrant Lock >> Reentrant Lock with Condition
Ok, so does that mean semaphore is pool of locks?
ReplyDeleteYes, we can say that. Actually Semaphore internally uses a AbstractQueuedSynchronizer which is a lock queue.
Delete