Synchronization and Locks
posted 15 years ago
Please help me understand why C and E are correct answers. The question is from K and B
Given:
And given these two fragments:
I. synchronized void move(long id) {
II. void move(long id) {
When either fragment I or fragment II is inserted at line 7, which are true? (Choose all that apply.)
A. Compilation fails
B. With fragment I, an exception is thrown
C. With fragment I, the output could be 4 2 4 2
D. With fragment I, the output could be 4 4 2 3
E. With fragment II, the output could be 2 4 2 4
Answer:
C and E are correct. E should be obvious. C is correct because even though move() is
synchronized, it's being invoked on two different objects.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Please help me understand why C and E are correct answers. The question is from K and B
Given:
And given these two fragments:
I. synchronized void move(long id) {
II. void move(long id) {
When either fragment I or fragment II is inserted at line 7, which are true? (Choose all that apply.)
A. Compilation fails
B. With fragment I, an exception is thrown
C. With fragment I, the output could be 4 2 4 2
D. With fragment I, the output could be 4 4 2 3
E. With fragment II, the output could be 2 4 2 4
Answer:
C and E are correct. E should be obvious. C is correct because even though move() is
synchronized, it's being invoked on two different objects.
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I think code is not complete.
Where the move method is being invoked.
Where the move method is being invoked.
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I think in place of
// insert code here
posted 15 years ago
The method is being invoked in run()
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Brij Garg wrote:I think code is not complete.
Where the move method is being invoked.
The method is being invoked in run()
posted 15 years ago
The answer E is obvious because if move() method is not synchronized, the output could be 2,4,2,4 or 4,2,4,2 or even 2,2,4,4 or 4,4,2,2. It is not guaranteed.
Even if you synchronize the move() method, the two threads created are not locking each other because they are invoked on two different Chess objects. So the output again could be 4,2,4,2 or 2,4,2,4 or 2,2,4,4 or 4,4,2,2. So E is correct
D is definitely not possible
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Ida Achi wrote:
Answer:
C and E are correct. E should be obvious. C is correct because even though move() is
synchronized, it's being invoked on two different objects.
The answer E is obvious because if move() method is not synchronized, the output could be 2,4,2,4 or 4,2,4,2 or even 2,2,4,4 or 4,4,2,2. It is not guaranteed.
Even if you synchronize the move() method, the two threads created are not locking each other because they are invoked on two different Chess objects. So the output again could be 4,2,4,2 or 2,4,2,4 or 2,2,4,4 or 4,4,2,2. So E is correct
D is definitely not possible
SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3,OCJP8 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
posted 15 years ago
synchronization is for object, not for method! And here, no methods are being invoked on object, other than move(long g), so there is no 'work' to synchronization!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Ida Achi wrote :
....... move() is synchronized, it's being invoked on two different objects.
synchronization is for object, not for method! And here, no methods are being invoked on object, other than move(long g), so there is no 'work' to synchronization!
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
| I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |







