• Post Reply Bookmark Topic Watch Topic
  • New Topic

Semaphores in Java  RSS feed

 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gurus,

We are designing a consumer / producer model, where I have one producer and 9 consumers. Is there any way by using Sempahore implementation in Java 6, where I can wake up a specific thread instead of waking up the other 8 threads.

Basically the requirement comes from the fact that the producer only has data relevant to a specific thread; waking up other threads will be a huge performance bottle neck.

Please advice.

-- Thanks Much
SM.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if there is an appropriate mechanism already available, but what you could do is have a shared variable keep track of the thread to wake up, and when you perform a notifyAll() you first set this variable to the thread to wake up. Then each thread will check this variable to see if they are the one to wake up, and if they aren't, they simply go back to sleep.

Here is a quick idea:
Here, wakeup is a static variable.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have every thread wait on a different object. Then you'll call notifyAll() on object assigned to the thread you want to wake.

Or, given that it is a producer/consumer model, set up 9 instances of BlockingQueue, each serviced by the specific thread, and have the producer put the job into the correct queue. BlockingQueue has already all you need.
 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch for the replies. Will setting the fairness to true help in any way ?.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Rodeo wrote:Thanks a bunch for the replies. Will setting the fairness to true help in any way ?.

Do you mean fairness setting of ArrayBlockingQueue in the scenario I've proposed? I don't think it would change anything in your case. As I understand it, the fairness setting is assured in every instance of the queue individually, not among multiple queues.

If there are more items in multiple queues, nothing actually guarantees that some of the thread would be given precedence simply by chance and other consumers could lag behind them. If you want to make sure this won't happen, you'll have to coordinate the consumers among themselves somehow. I cannot think of anything from the top of my head, but you should study java.util.concurrent package thoroughly. There are many very useful classes that you could employ.
 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

NO I mean the setting in the Semaphore constructor ( boolean true )
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your consumers will all wait on that semaphore and you need them to be serviced in the order in which they came, then fairness would ensure that. Without it there is no guarantee of their order whatsoever, whether it would be a problem for you remains to be seen. Generally fairness reduces throughput a bit, since it is more complex process than a simple unordered wait.

However, I don't see a straightforward solution of your original problem based on a single semaphore (you need to wake up a thread for which a job is ready, not the thread that waited longest), so whether the fairness would be useful in the final solution depends on the rest of your design.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!