• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Multithread program from khalid mughal

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell me where i am wrong in this program and how can i do it in a better wayBookmark:Question:
Create three Classes: Storage, Counter and Printer. The Storage class should store an integer. The Counter class should create a thread that starts from counting from 0(0, 1, 2, 3....) and stores each value in the Storage class. The Printer class should create a thread that keeps reading the value in the Storage class and printing it.
Create a program that create an instance of the Storage class, and sets up a Counter and a Printer object to operate on it. Modify the program to ensure that each number is printed exactly once, by adding suitable synchrnization

 
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

Modify the program to ensure that each number is printed exactly once, by adding suitable synchrnization



For code to "ensure that each number is printed exactly once", shouldn't there be a mechanism to track whether something is printed? Meaning.... Shouldn't the counter not store another number if the previous one haven't been printed yet? Shouldn't the printer not print the number, if it is the same number that was already printed?

Henry
 
Jacob Sonia
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i read the consumer producer problem and came across this solution but unfortunately it is not working exactly. Please help
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change lines 4 and 40 to "extends Thread" instead of implements Runnable. Conceptual problem in creating threads.

Regards
 
Jacob Sonia
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Done it but i am again at the same problem
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modified your original code as follows and it appears to work:

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jacob you need to understand basically how you should have used synchronization in this program. (BTW Ken your program will halt if the run method in Counter class runs before run in Printer class). You can use flags in your program to indicate whether a number has been printed or not. This is not necessary but it will take care of spontaneous thread wake-ups. Lets see this code



The flag named printed in the program will let Counter class know if the value has been printed yet. If it has not been printed, then Counter will wait till the value is printed then it will increase the value of i. For the Printer class, the flag will be used to ensure that the value is not printed more than once. The two while loops are there to take care of spontaneous wake-ups. I don't remember if you need to worry about that for SCJP or not. In spontaneous wake-up, a thread comes out of a wait call without any call to notify on the subsequent object. To decrease complexity, you can replace those loops with if conditions...
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic