aspose file tools*
The moose likes Threads and Synchronization and the fly likes Producer Consumer Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Producer Consumer Problem" Watch "Producer Consumer Problem" New topic
Author

Producer Consumer Problem

AnirbanDelhi Pal
Greenhorn

Joined: Jan 12, 2012
Posts: 6
Dear All,

I have implemented Producer Consumer problem taken hint from How To Program by H. M. Deitel. In book solution has given using object locking mechanism. Like private Lock accessLock = new ReentrantLock();
and declaring as private Condition canWrite = accessLock.newCondition();
private Condition canRead = accessLock.newCondition

Just for a try, I have not used any kind of object locking. Just uses a simple boolean variable just to denote either buffer is empty or full, based on that Producer writes or Consumer reads. And it worked. Why so ? Is not synchronisation has it's job to play in between ??

class 1


class 2



class 3


class 4

AnirbanDelhi Pal
Greenhorn

Joined: Jan 12, 2012
Posts: 6
Output :

Producer i = 1 sum = 1 status = false
Consumer i = 1 sum = 1 status = true
Producer i = 2 sum = 3 status = false
Consumer i = 2 sum = 3 status = true
Producer i = 3 sum = 6 status = false
Consumer i = 3 sum = 6 status = true
Producer i = 4 sum = 10 status = false
Consumer i = 4 sum = 10 status = true
Producer i = 5 sum = 15 status = false
Consumer i = 5 sum = 15 status = true
Producer i = 6 sum = 21 status = false
Consumer i = 6 sum = 21 status = true
Producer i = 7 sum = 28 status = false
Consumer i = 7 sum = 28 status = true
Producer i = 8 sum = 36 status = false
Consumer i = 8 sum = 36 status = true
Producer i = 9 sum = 45 status = false
Consumer i = 9 sum = 45 status = true
Producer i = 10 sum = 55 status = false
Consumer i = 10 sum = 55 status = true


If the sum for consumer comes anything other than 55, we can tell synchronisation has failed. but I have tried 5 - 6 times, every time Consumer gives output as 55 at last line.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

AnirbanDelhi Pal wrote:
Just for a try, I have not used any kind of object locking. Just uses a simple boolean variable just to denote either buffer is empty or full, based on that Producer writes or Consumer reads. And it worked. Why so ? Is not synchronisation has it's job to play in between ??


Thread safety just means that it will work correctly. The lack of thread safety means that it may not work correctly. It doesn't mean that it will definitely not work.

Try ...

1. Making the loop bigger.
2. Removing the sleep calls during each loop
3. On a different machine, with a different JVM version
3b. On a different machine, with a different processor or OS version
3c. On a different machine, with entirely different processors or operating system.

etc.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Henry Wong wrote:Try ...

1. Making the loop bigger.
2. Removing the sleep calls during each loop
3. On a different machine, with a different JVM version
3b. On a different machine, with a different processor or OS version
3c. On a different machine, with entirely different processors or operating system.

etc.

Henry

I'd just add
4. Running several consumers in parallel.

The sleep in the producer's loop makes everything flow smooth and nice. Adding one or more consumer to the game should bring some race conditions...
Anish Nath
Greenhorn

Joined: Sep 21, 2011
Posts: 23
how to Solve Producer Consumer Problem


The producer–consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Producer Consumer Problem