• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Help needed in cracking a program.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, ranchers!
I'm here yet again to ask a silly doubt (well, at least considering the the control statements used well in this program). This program was coded by one of the staff of my college, who is unavailable now due to the busy commitments in monitoring the ongoing computer lab exams conducted by the V.T.U. University of India.

Even though I know the basics of constructors, control and conditional statements, I request you ranchers to provide me a thorough, yet simplistic explanation of how this program works (also including how the constructors work in this program). The program is about demonstrating synchronization using the concept of producer-consumer relationship.

Thanks in advance.

P.S., I might ask some more questions regarding explanations of other already coded programs too, as I have Java lab examination yet again in M.C.A course on 24th Nov in the Indian calendar.

 
Saloon Keeper
Posts: 14275
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Product class is essentially a list of product numbers, except it can hold at most one product number at a time. When another product number is added, the old product number is overwritten.

Producers just set the product number to the values 1-10, without checking if one is already set. Consumers read the latest product number, but first have to wait for new product numbers.

This means producers can put multiple values in the time it takes a consumer to get one. Therefore, program executions may end in dead-lock, because the consumer tries to read 10 product numbers, but may miss product numbers that are being overwritten by the producer. In practice the chance is very low, because the producer produces one product number every second, which is an eternity in computer land.
 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The Product class is essentially a list of product numbers, except it can hold at most one product number at a time. When another product number is added, the old product number is overwritten.

Producers just set the product number to the values 1-10, without checking if one is already set. Consumers read the latest product number, but first have to wait for new product numbers.

This means producers can put multiple values in the time it takes a consumer to get one. Therefore, program executions may end in dead-lock, because the consumer tries to read 10 product numbers, but may miss product numbers that are being overwritten by the producer. In practice the chance is very low, because the producer produces one product number every second, which is an eternity in computer land.



Thanks for briefing me the working of this program! You helped me look at this program in an easier manner. I'll revert back if necessary.

But I am still mildly confused how the Product class works, especially declaring "available" as false and the "get()" function in that class. Would you please explain me that?
 
Stephan van Hulst
Saloon Keeper
Posts: 14275
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Product is poorly named. It would be better if it was called ProductHolder. There's actually a lot more wrong with this code. I suggest you don't try to imitate it.

Anyway, when get() is called, it checks whether a product number is available. While it isn't, it waits. After a product number comes available, the get() method first sets available to false to indicate that it has taken the most recent product number away, and then returns that product number.

The put() method on the other hand, sets the product number and then sets available to true, to indicate that there is a new product number available. It then calls notify() to tell the consumer to stop waiting at the wait() call.

Producers and consumers run at the same time. That's what Threads do. So while the consumer is waiting inside the get() for a product number to become available, at the same time the producer is making them available using the put() method.
 
Nikhilesh Puranik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.
Well, if you insist, I will try altering the code to my knowledge.
 
Don't play dumb with me! But you can try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic