• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help me with synchronized stuff

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:

public class SyncTest{
private int x;
private int y;
private synchronized setX (int i) {x=i;}
private synchronized setY (int i ){ y=i;}
public void setXY (int i ) {setX(i);setY(i);}
public synchronized boolean check() { return x!= y;}
}
Under which conditions will check() return true when called from a different class?
a. check() can never return true.
b. check() can return true when setXY is called by multiple threads.
c. check() can return true when multiple threads call setX and setY seperately.
d. check() can only return true if SynchTest is changed to allow x and y to be set seperately.
Which is the answer? I am always confused by synchronized stuff. Please help to explain the condition in this question. Thank so much.
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question really has nothing to do with synchonized methods. The only possible way for check() to return true is when x!=y, and that can happen only when x and y are set SEPARATELY.
So, the answer to your question is C.
 
Han Shu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but I still cannot figure it out.
Is there any possibility like this: after one thread just finishes setX(), it will release the monitor, and meanwhile the other thread can get the monitor and grasp CPU in turn, which runs check() at once, then it seems that x and y are not equal at that time.
Thank you for helping me!
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you call setX() and setY() in identical manners in 2 separate Threads, yes, it is possible when check() is called, catching the two Threads in a non-equivalent progress, and x !=y.
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic