Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes Producer and consume optimistic code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Producer and consume optimistic code" Watch "Producer and consume optimistic code" New topic

Producer and consume optimistic code

siddhu Math
Ranch Hand

Joined: Aug 10, 2006
Posts: 46

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
Ranch Hand

Joined: Apr 05, 2010
Posts: 50
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

Joined: Aug 10, 2006
Posts: 46
I was just expecting any algorithm that can handle this issue in a much better way...
Pat Farrell

Joined: Aug 11, 2007
Posts: 4646

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

Joined: Sep 28, 2004
Posts: 18538

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Don't get me started about those stupid light bulbs.
subject: Producer and consume optimistic code
Similar Threads
Producer/Consumer Design impacts exception handling
Problem with Consumer/Producer implementation
Thread wait() and notify()
Should Threads performing independent tasks sleep?
regarding producer/consumer example