• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Producer Consumer Problem

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic