• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

a Synchronized qusetion

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's your guess?
 
guo mark
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think x,y are private so .....
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3x for you help
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
J Hreich
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even thought we might see this in all different platforms, it is still not guranteed.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic