wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Multi Thread Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multi Thread Question" Watch "Multi Thread Question" New topic
Author

Multi Thread Question

Roger Zhao
Ranch Hand

Joined: Aug 05, 2003
Posts: 73
Hi, all:
Given:
1. public class SyncTest {
2. private int x;
3. private int y;
4. private synchronized void setX( int i ) { x = i; }
5. private synchronized void setY( int i ) { y = i; }
6. public void setXY( int i ) { setX(i); setY(i); }
7. public synchronized boolean check() { return x != y; }
8. }
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.
I choose B. Right? Check,SetX,SetY are synchronized methods and x,y are private vars, but SetXY is normal method. The value of x,y can be set different when multi-thread calls SetXY.
Anybody give me a full explanation about it?

Thanks a lot,
Roger


"There is a will,there is a way!"<br />SCJP1.4
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

B is probably the closest answer, but this is a badly worded question. The right answer is the non-existent E, "check can return true when setXY() and check() are called from different threads, nearly simultaneously."


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multi Thread Question
 
Similar Threads
Question on synchronisation
Question on threads
synchronize
Qn on Threads