• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Threads - doubt

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All!

public class Master {
boolean b = false;
public static void main(String args[]) {
Master m = new Master();
m.go();
}
public void go() {
Slave s = new Slave(this);
Thread t1 = new Thread(s);
t1.start();
while(b==false) {
}
s.setPrice(200);
}
}

class Slave implements Runnable {
int iPrice = 100;
Master master;
Slave(Master m) {
master = m;
}
synchronized public void setPrice(int iM) {
iPrice = iM;
}
synchronized public void run() {
master.b = true;
while(true) {
System.out.println(iPrice);
}
}
}

Ans --> Compilation and output of 100 several times followed by 200 several times.

Pls explain.
Thanks
Neha Monga
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Follow this link:


Same Example, discussed here


Thanks,
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that it is printing 200??
It will not print 200 as the while loop in the Slave class never returns. It will print 100 endlessly.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the program will print 100 endlessly because of the following lines

 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Raghavan.
It is not because of while loop,
It is because of the synchronized kye woed in the setPrice method.
Remove that synchronized key word from that method, you will get 200.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the same.

Thanks for pointing it out Krishna!
 
Neha Monga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra for directing me to the earlier discussed question!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic