Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread & Lock

 
WiLL Tao
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 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.

--------------------------------------------
To this question,my answer is C, is it right?
and i think LOCK relate Object, not method.
Hence if one thread access setXY method or check method (both are Synchronized), the Object refence which to SychTest cann't be accessed by others.No other methods can be invoked.(such as setX ,setY)
Am I right?if something wrong,Please let me know.Thanks.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well technically, this won't compile because Java is case sensitive, so Synchronized won't be recognized as a keyword.
But aside from that, you are correct that C is the right answer.
A synchronized block of code only prevents another thread from entering that bock if another thread already has a lock on that monitor object, so your last statement is NOT true. setX() and setY() are not synchronized, so there is no restriction about calling them. Any thread can call them at any time, multiple times, and that's why there's a potential invariant violation, ie, some other thread can modify the value of X while another thread is calling setXY() or check().
 
WiLL Tao
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:
Well technically, this won't compile because Java is case sensitive, so Synchronized won't be recognized as a keyword.
But aside from that, you are correct that C is the right answer.
A synchronized block of code only prevents another thread from entering that bock if another thread already has a lock on that monitor object, so your last statement is NOT true. setX() and setY() are not synchronized, so there is no restriction about calling them. Any thread can call them at any time, multiple times, and that's why there's a potential invariant violation, ie, some other thread can modify the value of X while another thread is calling setXY() or check().


Hi,Rob;thank you for reply and correct.
synchronized not Synchronized .Now i put this in heart.
You said setX() and setY() are not synchronized,so they can be called at any time.
But how about Object's lock?I remember that one Object only one lock?
Example


but if synchronized method be invoked.
eg: setXY(int i), then no other methods can be invoked.
Example

 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm not too clear about the answer D. Why is it wrong also?

D.check() can only return true if SychTest is changed allow x and y to be set separately.
 
Mag Hoehme
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since answer C is correct, answer D must be false: "can only return true if...". Both answers are mutually exclusive.
Another explanation could be: It is not a necessary condition that class SynchTest is able to modify its variables x and y for check() to return true. The reason is that both methods, setX(int i) and setY (int i), are public, and thus can be called independently of each other by any thread, causing (x != y) being true. The matter would be different if both methods were private and thus could only be accessed by the public setXY (int i) method.
Hope this helps.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same question has been posted yesterday
http://www.coderanch.com/t/237403/java-programmer-SCJP/certification/Synchronized-qusetion
Clement,
D is wrong because x and y are private members.
[ April 12, 2002: Message edited by: Valentin Crettaz ]
 
Mag Hoehme
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite the same: In yesterday's example the setX() and setY() methods were private; in this example they're public.
Therefore in yesterday's example these methods were not accessible from outside the class, and the variables x and y could only be modified via the synchronized setXY(). In this example the variables x and y can be modified by any thread calling this class's setX(int i) or setY (int i) method, allowing x and y being modified independently of each other.
Please let me know if I'm mistaken. Thank you.
[ April 12, 2002: Message edited by: Mag Hoehme ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic