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

help me this code

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me this code step by step :-..................
The out put is Compilation and repeated output of 100

public class Master{
boolean bContinue=false;
public static void main(String argv[]){
Master m = new Master();
m.go();
}
public void go(){
Slave s = new Slave(this);
Thread t1 = new Thread(s);
t1.start();
while(bContinue==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.bContinue=true;
while(true){
System.out.println(iPrice);
}

}
}
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karthik,

Here, as per my understanding, Slave's object has got locked because of synchronized run method. So, when the Thread starts, run method is in infinite loop. And lock on the Slave object will not be free. In that case, you will not be able to change any of the attribute vaules there. The main Thread will be "blocked" for the setPrice operation in go().

Hope it will help,

Regards,
Mausam
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your understanding of the code Karthik?
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread t1 acquired the lock of s1, hence main thread can't access setPrice method of Slave s1, because

1. The lock held by T1 and is not released.
2. And setPrice is synchronized method on Slave, which means that if any thread wants to invoke this method should get the lock first. Here in this case main thread will be waiting for the lock to be released by T1, which wont happen as per the code.
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread t1 acquired the lock of s1, hence main thread can't access setPrice method of Slave s1, because

1. The lock held by T1 and is not released.
2. And setPrice is synchronized method on Slave, which means that if any thread wants to invoke this method should get the lock first. Here in this case main thread will be waiting for the lock to be released by T1, which wont happen as per the code.
 
reply
    Bookmark Topic Watch Topic
  • New Topic