This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Producer Consumer Problem

 
AnirbanDelhi Pal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21511
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic