wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronization ???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronization ????" Watch "Synchronization ????" New topic
Author

Synchronization ????

Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
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

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Mar 20, 2002
Posts: 114
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Sorry guys, forget that "private" thing... I guess, I need a good night sleep
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
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

Joined: Jun 04, 2002
Posts: 133
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Synchronization ????