*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread & Lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread & Lock" Watch "Thread & Lock" New topic
Author

Thread & Lock

WiLL Tao
Ranch Hand

Joined: Dec 14, 2001
Posts: 56

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.


Thank Folks who <b>Make Sense</b> here.<br />SCJP Platform 2
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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().


Rob
SCJP 1.4
WiLL Tao
Ranch Hand

Joined: Dec 14, 2001
Posts: 56
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

Joined: Mar 07, 2002
Posts: 295
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.


best regards,<br />Steven<br />SCJP, SCEA
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
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.


Mag
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread & Lock
 
Similar Threads
Qn on Threads
Multi Thread Question
Threads doubt from mock
Synchronized methods
a Synchronized qusetion