• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Question Doubt...EQ+...basics

 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,

Question ID : com.enthuware.ets.scjp.v5.2.396

If a synchronized method throws an exception in its execution, any locks accquired by the method are released automatically.

TRUE/FALSE..

The answer is true...and it gives the explanation..

" The Java exception mechanism is integrated with the Java synchronization model, so that locks are released if synchronized statements and invocations of synchronized methods complete abruptly."

My question is what if the exception is handled in the synchronized method by try/catch..??

Then the answer to the above question would be FALSE...
.........What do you think ?


Below example demonstrates this




Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hardik,


You are right.


The Java exception mechanism is integrated with the Java synchronization model, so that locks are released if synchronized statements and invocations of synchronized methods complete abruptly."



Consider the word abruptly in the description of JQ+.
Lock is released if the exception is not handled of-course.


Thanks,
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your synchronized method run() has not thrown an exception if you catch it inside run(). So the lock does not need to be released.
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks..got it.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic