This is not a very good documented feature in Java. And if you search the web for when to use finally you will find numerous example of using finally for closing resources. And these are good example of when to use finally, but what they will not tell you is that the finally code will run in a separate thread with low priority. This means that if you put logic in your finally block this code will not be thread safe. No matter what you do to make it thread-safe.
The same thing goes if you are depending on the state of the resource. For example if you are building a state machine that should change state whenever a resource is open or closed. And you are relying and whichever the resource is open or closed by calling the resource itself. This will not be thread-safe if you close the resource in a finally block.
Putting Logic in finally is not Thread-safe
January 23, 2012
Posted by Magnus K Karlsson at 9:14 AM
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment