• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finally and Finalize

 
Jim DiCesare
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering when it is necessary to use finally. I've read other posts that imply that a method will be atomic, and not get interrupted by another process in the middle. For this reason I used finally in my class that uses the database, and I always call unlock within the finally block so an interruption or exception will not leave locked records. Is this enough use of finally, or are there other places its important?
If a method is not atomic, do I need to worry about rolling back incomplete transactions if the server crashes or loses connection. So if my update method updates half the fields, then gets interrupted, do I need to roll back the changes?
Also, I was wondering if I need to use finalize. Is it important to make objects garbage collectable as soon as possible, or do I not need to worry about this at all?
 
Kerry Friesen
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings Jim,

Do not worry about using finalize.

You may use the final keyword on a class if you want to. It makes the class non-extendable, but does not guarantee methods will execute atomically. If you wish your methods to perform atomically and not get interrupted in the middle of execution, you must add the synchronized keyword to the method signature.

I would not worry about adding the final keyword to your classes, methods, or variables for this assignment. There is much debate/opinion about the performance gains due to the final keyword or for making a class non-extendable. I believe adding it would potentially make the code more difficult for a junior programmer to understand.

Those are my thoughts. Anyone else?

Regards,
Kerry
[ December 02, 2005: Message edited by: Kerry Friesen ]
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,
Jim:
I am wondering when it is necessary to use finally. I've read other posts that imply that a method will be atomic, and not get interrupted by another process in the middle. For this reason I used finally in my class that uses the database, and I always call unlock within the finally block so an interruption or exception will not leave locked records.

There is no construct in Java which would garantee that the execution of a piece of code will not be "interrupted" : the thread scheduler may - at any time - decide to put your thread from the "running" to the "runnable" state. Even the "synchronized" construct cannot avoid that.
But try .. finally garantees that whatever (except System.exit()) happens between the try and the finally (an uncatched exception is thrown for instance), the finally block will execute.
Finally is typically used to ensure that a resource acquired in a method will be released in that method. A lock is such a resource, so I think that unlocking in a finally block is good and safe practice.
Kerry:
I would not worry about adding finally to your (...) methods. (...) I believe adding it would potentially make the code more difficult for a junior programmer to understand.


try .. finally is a basic Java construct ! If a junior programmer doesn't understand it, it's a high time that he learns it. And reading correct and clean Java code is the best way junior programmers have to learn more IMO.
Best,
Phil.
 
Kerry Friesen
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops... I mistook finally for the "final" keyword. My bad.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic