wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Question on Semaphores Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Question on Semaphores" Watch "Question on Semaphores" New topic
Author

Question on Semaphores

Dinakar Kas
Ranch Hand

Joined: Jul 11, 2010
Posts: 34

means that there can be a maximum of N permits on a resource. So, N threads can access a resource at one point of time.
What does it mean if I say



From the above it means that it has 0 permits and no threads can acces it.

Now, what happens if I try to acquire a permit by saying sem.acquire();??

Thanks.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

Dinakar Kas wrote:
Now, what happens if I try to acquire a permit by saying sem.acquire();??


Keep in mind, that the Java Semaphore class is not a lock (although it can be used to simulate a lock). It's just a simple counter that holds permits. Any thread can acquire a permit, and any number of permits. Any thread can release any number of permits. And there are no checks to confirm that a thread that releases a permit actually acquired it.

So, it's perfectly reasonable to have a semaphore with an initial permit count of zero, or even negative. And the semaphore won't allow any thread to acquire any permits until the count becomes positive.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dinakar Kas
Ranch Hand

Joined: Jul 11, 2010
Posts: 34
Any thread can acquire a permit, and any number of permits


Thanks Henry, But doesn't it need to be below maximum number of permits available?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

Dinakar Kas wrote:
Thanks Henry, But doesn't it need to be below maximum number of permits available?


What do you mean by "maximum number of permits"?

Henry
Dinakar Kas
Ranch Hand

Joined: Jul 11, 2010
Posts: 34
Maximum number of permits are number of available permits which we specify in semaphore declaration.

In



Maximum number of permits available are N.

Am I clear?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

Dinakar Kas wrote:Maximum number of permits are number of available permits which we specify in semaphore declaration.

In



Maximum number of permits available are N.

Am I clear?



As already mentioned, that is *not* the maximum number of permits. That's the initial number of permits.

It is not required for a thread to have acquired a permit before releasing it. So it is perfectly fine to try to acquire a number that is greater than N, assuming that you have another thread that will "create more permits" (by calling release()).

Henry
Dinakar Kas
Ranch Hand

Joined: Jul 11, 2010
Posts: 34
Thanks a lot Henry. I got it.

Dinakar
Dinakar Kas
Ranch Hand

Joined: Jul 11, 2010
Posts: 34
If I am not asking for more, Can you please let me know if Iam implementing the locking correctly using semaphores in the below Producer/Consumer Program.



I am asking this because I came upon a code which was using two semaphores, one for Producer and one for Consumer and I thought that I could do it with one.

The code is as follows:



The above code is taken from java2s.com.

Thanks,
Dinakar
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

Dinakar Kas wrote:
I am asking this because I came upon a code which was using two semaphores, one for Producer and one for Consumer and I thought that I could do it with one.


Sure, you can give it a try -- but keep in mine that with two semaphores, are used for both state and signaling. If you turn it to only one semaphore, used only for signaling, then you may need to add a state flag.

Also, there are the built-in BlockingQueue classes. There is really no reason to write your own.

Henry
Dinakar Kas
Ranch Hand

Joined: Jul 11, 2010
Posts: 34
Hi Henry,

Thanks a lot.

I have implemented Producer/Consumer program using BlockingQueue. However, I wanted to get hands dirty with Semaphores.

Also, I want to write a blog on thread basics, synchronization techniques, and advanced synchronization techniques so that it will help others. Can I send you the document for review before I post it?

Dinakar.K
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Semaphores