aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K and B Chapter 9 self test qn no 17 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 "K and B Chapter 9 self test qn no 17" Watch "K and B Chapter 9 self test qn no 17" New topic
Author

K and B Chapter 9 self test qn no 17

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69

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.)
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:
3 C and E are correct.
I thought the answer is E.But both C and E.How cum it possible..we are using syncronization in Fagment 1 which locks the object.
then how can it acquire the lock of another object??
i searched the posts but couldn't get it.
please explain in detail thanks in advance
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

here we are creating two threads and we are passing two different runnable targets to these two threads. so even though there is synchronized keyword in the method move() but the two threads are acquiring locks on two different chess objects. so two threads will be running concurrently. keep in mind that when a thread enters a synchronized method, it will acquire lock on the object currently running the method i.e. this. so in two thread both threads are getting locks on different chess objects and hence the output.
kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
gurpeet singh wrote:here we are creating two threads and we are passing two different runnable targets to these two threads. so even though there is synchronized keyword in the method move() but the two threads are acquiring locks on two different chess objects. so two threads will be running concurrently. keep in mind that when a thread enters a synchronized method, it will acquire lock on the object currently running the method i.e. this. so in two thread both threads are getting locks on different chess objects and hence the output.

thanks alot for the explanation..i got the point now
 
jQuery in Action, 2nd edition
 
subject: K and B Chapter 9 self test qn no 17