• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on Semaphores

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic