This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a  Synchronized  qusetion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a  Synchronized  qusetion " Watch "a  Synchronized  qusetion " New topic
Author

a Synchronized qusetion

guo mark
Ranch Hand

Joined: Oct 20, 2001
Posts: 43
I got a question like that
public class SyncTest{
private int x;
private int y;
private Synchronized void setX(int i){x=i;}
private Synchronized void setY(int i){y=i;}
public void setXY(int i){
setX(i);
setY(i);
}
public Synchronized boolean check(){
return x!=y;
}
}
what is return of check()?
A.check() can never return true.
B.check() can return true when setXY is callled by multiple threads.
C.check() can return true when multiple threads call setX and setY separately.
D.check() can only return true if SychTest is changed allow x and y to be set separately.

which one is right ?


meet once ,love every while
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
What's your guess?


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

Joined: Oct 20, 2001
Posts: 43
i think x,y are private so .....
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
that's right x and y are private but the question is "is it possible that x and y be assigned different values if different threads play around with the SyncTest class?" Have a careful look at which methods are synchronized and which ones are not...
guo mark
Ranch Hand

Joined: Oct 20, 2001
Posts: 43
3x for you help
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You know, the best way to learn that stuff is to run the code. Although thread behaviour is implementation-dependent you can still deduce important things. For instance, the code below shows you that answers A and D are clearly wrong.
Then you just have to tweak it a little bit to choose between B or C.
J Hreich
Ranch Hand

Joined: Mar 22, 2002
Posts: 37
Option B is obviously correct, as one can
see from Val's sample code. The main thing
to know is that if setXY() is not synchronized.
i.e. A thread executing setXY() can be pre-
empted after setX() is called but before
SetY() is.
As for option C, I also think it is correct
for the same reason as above. To force the
output of check() to be false all the time,
the run() method calling setX() and setY()
must be synchronized.
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
..I don't think C will work though, unless you do something inside the class. The setX() and setY() are private.
J Hreich
Ranch Hand

Joined: Mar 22, 2002
Posts: 37
Michael,
A call to sleep() only allows the scheduler to get another thread running (again, not guaranteed). The samething could happen without having to call sleep (again, not guranteed).
Thus, A is always wrong (with a call to sleep or not).
Michael Pavlov
Greenhorn

Joined: Mar 24, 2002
Posts: 12
Jay,
I checked the fallowing code both on Unix and Linux, and I got ONLY false to the output!!!; it
remains only the Windows platform, but if the output it's different I should change my motto to "Programm once, run everywhere. NEVER bet on it!"
Just joking, here is the code:
-----------------------------------------------
public class SyncTest implements Runnable{
private int x;
private int y;
private synchronized void setX(int i){x=i;}
private synchronized void setY(int i){y=i;}
public void setXY(int i){
setX(i);
//try{
// Thread.sleep(100);
//}catch(Exception e){}
setY(i);
}
public synchronized boolean check(){
return x!=y;
}
public void run(){
for(int i=0;i<100;i++){
setXY(i);
System.out.println(x+","+y+","+check());
}
}
public static void main(String[] args){
SyncTest t = new SyncTest();
Thread[] ts = new Thread[10];
for(int i=0;i<ts.length;i++){
ts[i]=new Thread(t);
ts[i].start();

String ourThreads = ts.toString();
System.out.println("< " + ourThreads + " >");
}
}
}
-----------------------------------------------
Did you tried to comment out the sleep() and run it?
Thanx,
Michael


"The artist is nothing without the gift, but the gift is nothing without work." <br /> - Emile Zola (1840-1902)
J Hreich
Ranch Hand

Joined: Mar 22, 2002
Posts: 37
Even thought we might see this in all different platforms, it is still not guranteed.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Ok once and for all, the correct answer is B. A and D have been proven wrong by the code I posted before and C is nonsense since setX and setY are private methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a Synchronized qusetion