Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Semaphores

 
Dinakar Kas
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dinakar Kas
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Henry. I got it.

Dinakar
 
Dinakar Kas
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic