Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization ????

 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Happy implements Runnable {
private int x;
private int y;
public void run() {
//some code
}
public static void main(String args[]) {
Happy h=new Happy();
new Thread(h).start();
new Thread(h).start();
}
void setX(int i) {
x=i;
}
void setY(int j) {
y=j;
}
synchronized void setXY(int i) {
setX(i);
setY(i);
}
synchronized boolean check() {
return x!=y;
}
}

Will this code ever return true. According to me NO. I tried various options.
If some one disagrees with me can u give me the code in which case it would print true.
Thanks
Deepali
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you removed from the run() method seems very important to me otherwise your methods setXY and check are never invoked... Could you please post that code...
[ June 20, 2002: Message edited by: Valentin Crettaz ]
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not removed anything the original ? is just as i have put. But some ppl have given some suggesstion here
http://www.coderanch.com/t/235022/java-programmer-SCJP/certification/Quession-obout-synchronized
I have tried all this and much more but dont understand when it can return true.
In teh above link u will see posts and replies goin on abt 2-3 different programs. let us stick to only this one here to avoid confusion
Thanks
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setX and setY are not synchronized. If any thread invokes them, then an invocation to check() could return true as well as false. If setX and setY are made private, then true could not be returned, but otherwise it could.
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Valentin Crettaz:
setX and setY are not synchronized. If any thread invokes them, then an invocation to check() could return true as well as false. If setX and setY are made private, then true could not be returned, but otherwise it could.

I still dont get it.I tried it with three threads and called this.setX(1);this.setY(1);
in run(). But if i see the computation happening at these methods there is no way it can return true. coz it just assigning x=i and y=j.
Are u trying to tell if the program was changed in someway then would return true.
Also tell me by making them private what are we achieving.
Thx
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what Val meant by making the methods private. If, for some reason, you couldn't invoke setX or setY, then this question would be different but, as these methods are all in the same class, making them private won't prevent them from being executed.
Anyway, as no code is supplied in the run method, we have to assume that any code could be put in there. How about if that code read, simply:

I guess that would probably cause x and y to be unequal, causing check to return true.
Corey
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys, forget that "private" thing... I guess, I need a good night sleep
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do u agree that it would return true only if setX() and setY() were called individually in run(). And for all other cases including the case were all 3 methods setX(), setY() and setXY() would be called from run() would return false.
The case were all 3 methods setX(), setY() and setXY() would be called from run() I assuem there is a possibility of returing true as 2 methids are unsync and one is sync???
Do such ? appear in exams. I mean with run() being empty.

Thanks
Originally posted by Corey McGlone:
I'm not sure what Val meant by making the methods private. If, for some reason, you couldn't invoke setX or setY, then this question would be different but, as these methods are all in the same class, making them private won't prevent them from being executed.
Anyway, as no code is supplied in the run method, we have to assume that any code could be put in there. How about if that code read, simply:

I guess that would probably cause x and y to be unequal, causing check to return true.
Corey
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any case that involves setX() or setY() being invoked outside of setXY() will create the possibility that check() will return true. Actually, since there is only one class involved, you can even set the values of x and y directly from the run() method!
The only solution I can think of for check() to never return true is to write the run() method and the x,y methods/vars in two separate classes. Then you can make (setX()/setY()/x/y) private so they can't be accessed outside the class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic