Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

semaphore explanation  RSS feed

 
nitiish chinnaraju
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the semaphore's acquire() method, To what the permit refers and also give me the explanation of the acquire() ?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36396
453
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The beginning of the java.util.concurrent.Semaphore JavaDoc says:

A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each acquire() blocks if necessary until a permit is available, and then takes it. Each release() adds a permit, potentially releasing a blocking acquirer. However, no actual permit objects are used; the Semaphore just keeps a count of the number available and acts accordingly.


The Semaphore allows a certain number of activities to do something at the same time. Suppose I have two unisex restrooms. I only want one person to be in each at a time. To deal with this, you have to get a physical key to the restroom to use it and return the key when you are done. (My public library used to operate this way.)

A Semaphore is similar. It has logical permits - the key in my example. When you want to use the resource, you call acquire() get that permit. This corresponds to going to the desk to get a key. When you are done, you call release() to allow the permit to be used by someone else. This corresponds to going back to the desk to return the key.
 
nitiish chinnaraju
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:The beginning of the java.lang.Semaphore JavaDoc says:

A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each acquire() blocks if necessary until a permit is available, and then takes it. Each release() adds a permit, potentially releasing a blocking acquirer. However, no actual permit objects are used; the Semaphore just keeps a count of the number available and acts accordingly.


The Semaphore allows a certain number of activities to do something at the same time. Suppose I have two unisex restrooms. I only want one person to be in each at a time. To deal with this, you have to get a physical key to the restroom to use it and return the key when you are done. (My public library used to operate this way.)

A Semaphore is similar. It has logical permits - the key in my example. When you want to use the resource, you call acquire() get that permit. This corresponds to going to the desk to get a key. When you are done, you call release() to allow the permit to be used by someone else. This corresponds to going back to the desk to return the key.



answer which you gave is useful... one question if i have semaphore sem =new semaphore(3), then how much permit should acquire() be given lesser than 3 or any numbers(greater than 3 also)?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36396
453
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nitiish chinnaraju wrote:answer which you gave is useful... one question if i have semaphore sem =new semaphore(3), then how much permit should acquire() be given lesser than 3 or any numbers(greater than 3 also)?

Acquire() is only given one permit out of the available three.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!