wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Question Doubt...EQ+...basics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Question Doubt...EQ+...basics" Watch "Mock Question Doubt...EQ+...basics" New topic
Author

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

Hardik Raja
Ranch Hand

Joined: Feb 07, 2006
Posts: 117
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

Joined: Feb 28, 2007
Posts: 1707
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,


cmbhatt
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Your synchronized method run() has not thrown an exception if you catch it inside run(). So the lock does not need to be released.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Hardik Raja
Ranch Hand

Joined: Feb 07, 2006
Posts: 117
hey thanks..got it.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock Question Doubt...EQ+...basics
 
Similar Threads
Threads blocking on static method.... or do they?
Regarding Threads
Practice question on synchronized
Thread Question about sleep method
Basic thread doubt