aspose file tools*
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 Java 8 in Action this week in the Java 8 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: 4639

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: 18141

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)
I agree. Here's the link:
subject: Producer and consume optimistic code
Similar Threads
regarding producer/consumer example
Should Threads performing independent tasks sleep?
Problem with Consumer/Producer implementation
Producer/Consumer Design impacts exception handling
Thread wait() and notify()