aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronization and Locks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronization and Locks" Watch "Synchronization and Locks" New topic
Author

Synchronization and Locks

Ida Achi
Greenhorn

Joined: Oct 02, 2009
Posts: 25

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.
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
I think code is not complete.
Where the move method is being invoked.
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

I think in place of

// insert code here
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Brij Garg wrote:I think code is not complete.
Where the move method is being invoked.


The method is being invoked in run()
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

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 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066


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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronization and Locks