Sometimes, you may have requirements to allow more than one thread to use a resource, means, not all threads but allowing 2 or 3 or 5 threads to use the resource at the same time & rest threads will need to wait till any one thread releases the lock.
One can do this in other way also, but here I am showing a way using Semaphore in Java-
Here I am creating semaphore with 3 permits i.e. 3 threads can take the lock at any moment of time, not more than 3.
One can do this in other way also, but here I am showing a way using Semaphore in Java-
Here I am creating semaphore with 3 permits i.e. 3 threads can take the lock at any moment of time, not more than 3.