Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K and B Chapter 9 self test qn no 17

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic