Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Producer and consume optimistic code

 
siddhu Math
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...


I was just thinking that what would be the most optimistic design for the issue say if I have N number of producer threads and N number of consumer threads and consumer will act only when the producer has produces irrespective of which thread has produced and which thread is consuming, for this just a while loop or a for loop would not be enough to make the code most optimistic that is what I feel so if any one would like too share there comments about this please?
 
Sergey Babkin
author
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easy way is to have a single queue where the produces post their requests, and consumers read them. Would work OK if the requests take a long time to process compared to the time it takes to read then from the queue.
 
siddhu Math
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just expecting any algorithm that can handle this issue in a much better way...
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't mean "optimistic" here do you?

Your exact case is not very interesting. Generalize it a bit, you have N threads/workers and M incoming requests. As long as N > M, then its not important what you do.

The interesting case is those times when N < M and especially when N is a lot less than M. This is exactly the reason that Java has the Thread pool concepts.
 
Henry Wong
author
Marshal
Pie
Posts: 20989
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:You don't mean "optimistic" here do you?


Agreed. "Optimistic" has a very specific meaning in terms of locking.

Also, "producer consumer" has a very specific meaning (aka pattern). And it generally involves an undetermined number of threads producing data, and an undetermined number of threads consuming the data. And the data communication is generally done via some sort of data structure, such as a queue.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic