• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

question on Synchronized

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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; }
}

Under which condition 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 separately.
D. check can return true only if SyncTest is changed to allow x and y to be set separately.

Answer: B. check can return true when setXY is called by multiple threads.

I am not 100% sure about this concept and why B is correct, is it because setXY is not synchronized.

Regards
JP
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reason:

You may or may not get true for check method even if you use multiple threads to call setXY() method. The reason is, no thread is willing to give the CPU cycles to other threads to do their work (i.e) sleep/yield should be there so that other threads can also do their job. If the underlying OS uses time slicing mechanism, then you may get true for check method. \

I modified your code, sothat check can return true.

Note: Only SetX and SetY are synchronized methods, not SetXY. Hence it can be called by more than one thread at a time.

 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see your point, so bottom line it is OS dependent as to whether the code I listed initially returns true. Now my question is can we expect such questions on the exam and if should what should be our answer ?

Regards,
Remi
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check can return true when setXY is called by multiple threads.

You may have to note the word here, it is "can return" not "will return". So the
right answer is B.


Now my question is can we expect such questions on the exam and if should what should be our answer ?



I am not a SCJP, ) Just started like you. But the ans should be B
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic