• 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

synchronization: Clarify!!

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clarify synchronization: I guess I didn't quite get hold off which object will be synchronized in this context.
Does both the run & setPrice methods are synchronized on the same object, confusing ?... any help is appreciated...
Thanks,
Shalini
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: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Taurean,
when you apply the keyword synchronized to a method, the whole method is synchronized using the corresponding object.
So, perhaps we can go through your code above?

after t1.start(), the main thread will be in the loop of line 1 above until bContinue is false. Meanwhile, thread t1 is executing the run() method of s, synchronized using s.

Since the run() method is synchronized, thread t1 will first acquire the lock of object s - apparently nobody else hold the lock of object s at this point. Then, at line 3, in thread t1, the bContinue of master is set to be true.
Now, since supposedly, both thread t1 and the main thread are running, after line 3, the loop in the main thread (line 1) will finish.
The main thread, then, is trying to execute s.setPrice(200) - line 2. But, since setPrice is also synchronized, it requires the lock of object s, which is already acquired by t1. So, the main thread has to wait until the lock on object s is release before it enter method setPrice(200) of object s. Since, line 4-6 are infinite loop, the main thread is going to wait forever for the lock of s.
So, if my analysis is correct, you will get print of lots of 100's (the initial value of iPrice), and never get the print of 200.
Hope this helps.
 
Ashok Paulraj
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, u got it David !!
~ Shalini
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi shalini,
after the statement t1.start(); is executed
the run method in Slave class gets executed by the thread t1 whereas your main thread is looping continuously here:
while(bContinue==false)
{
}
because both run() and setPrice() are synchronized methods of the same class if any thread enters one of the methods automatically all synchronized methods of that object ("s object" here) are locked by that thread which means no other thread(main thread here) can enter the synchronized methods using the same "object s".
now the thread t1 sets the value of master.bContinue to true(master.bContinue=true so the main thread comes out of the above while loop and tries to execute
s.setPrice(200);
but remember out little s objects' synchronized method setPrice() is locked by the thread t1 in the run method and looping infinitely here:
while(true){
System.out.println(iPrice);
}
So the thread t1 will be in the running state forever and never gives up the s objects' lock while the main thread will be in runnable state for eternity.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic