Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

question on synchronization  RSS feed

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai ranchers
can any one expalin this question


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);
}
}
}

by
velan vel
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by velan vel:
... can any one expalin this question...


That's a question?

Well, Slave's run method is synchronized, so the thread calling that method must obtain the object lock before the method can execute. Once this happens, it enters the "while(true)" infinite loop and continues to print the value of iPrice.

But Slave's setPrice method is also synchronized, so if a thread is stuck in the infinite loop (holding the object lock), then setPrice cannot be invoked by another thread because the object lock cannot be obtained.

The while(bContinue==false){} loop ensures that s.setPrice(200) will not be called until thread t1 has already obtained the object lock and is in the run method (where bContinue is changed to true). So the only way I can see that a value of 200 would be printed rather than 100 is if the thread scheduler gave the main thread a chance to run immediately after setting bContinue to true, but before entering the infinite loop. This seems doubtful, but I suppose it's possible.

:roll:

PS: In the future, please format your code and use Code Tags. Here's a formatted version for anyone else following this...

(Once running, use Ctrl+C to break out of the infinite loop. )
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Marc, except one thing:

So the only way I can see that a value of 200 would be printed rather than 100 is if the thread scheduler gave the main thread a chance to run immediately after setting bContinue to true, but before entering the infinite loop. This seems doubtful, but I suppose it's possible.


As he wrote earlier run method is synchronized. It obtains lock before it sets bContinue to true and it still owns it even when thread scheduler gave the main thread a chance to run.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!