Posts

Showing posts with the label concurrent

What is Semaphore with example ?

What is Semaphore? Semaphore is used to control access to common resource for completing multiple resources . It guards a critical section against entry by more than N threads at a time. The  java.util.concurrent.Semaphore   class was first introduced by Java in V 1.5. It has two main methods(): ·          acquire() ·          release() Semaphore is initialized with a given number of "permits” i.e. counter which keeps track of the number of resources available. When a request comes to resources, Semaphore checks the counter and if it is less than total count then it grant access to resources and subsequently reduces the counter. Similarly while releasing a resources, it increments the count. Thus, at most N threads can access the acquire method without any release() calls where N is number of permits the semaphore was initialized with. If the permits is in ON|OFF mode i....