Reputation: 81
Experienced programmer new to Java seeking your wisdom:
If there is no way to ensure that some particular chunk code is executed as an object goes out of scope, then what other approaches are there that would offer the same functionality? (it seems finalize is clearly not meant for that)
A classic example is the scoped lock idiom:
void method()
{
// Thread-unsafe operations {...}
{ // <- New scope
// Give a mutex to the lock
ScopedLock lock(m_mutex);
// thread safe operations {...}
if (...) return; // Mutex is unlocked automatically on return
// thread safe operations {...}
} // <- End of scope, Mutex is unlocked automatically
// Thread-unsafe operations {...}
}
I can understand that in Java it would be frowned upon to have some code executed if you haven't explicitly called it. But I find being able to enforce some code to be executed at the end of an object's lifetime is a very powerful feature to ensure your classes are being used sensibly by client code. Thanks
Upvotes: 7
Views: 477
Reputation: 65851
Generally - if you have a need to actually close/dispose of resources then using a try{} finally{}
structure is encouraged.
// The old way - using try/finally
Statement stmt = con.createStatement();
try {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
// ...
}
} catch (SQLException e) {
// Whatever ... .
} finally {
// Be sure to close the statement.
if (stmt != null) {
stmt.close();
}
}
More recent incarnations of java have the AutoCloseable interface which you can use with the with
mechanism. All Closeable objects are automatically AutoCloseable
.
// Example of what is called try-with
try (Statement stmt = con.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
// ...
}
} catch (SQLException e) {
// Whatever ... stmt will be closed.
}
Upvotes: 9
Reputation: 903
If you want some code to be run anyway in a METHOD, just use try... finally.... The finally block is guaranteed to run. Or, in Java 7, uses the "with" block.
If you want some code to be run like the destructor of C++. I am afraid there is no such a thing in Java. The finalize method is not reliable and could not be used to handle critical missions. The way Java classes normally do is to expose a clean up method (e.g. close() of those stream classes) so that the client calls these method explicitly to do cleanup jobs.
Upvotes: 1
Reputation: 7940
Another option to replace finalize
method is PhantomReference
.
If you want to perform some action before object is garbage colected then it offers another good approach better than finalize
method.
Check for example here : https://weblogs.java.net/blog/kcpeppe/archive/2011/09/29/mysterious-phantom-reference
Upvotes: 1
Reputation: 200206
As of Java 7 there is the Automatic Resource Management. If your lock is under your control, then make it implement AutoCloseable
. Unfortunately, the java.util.concurrent
locks do not implement this interface. Here's a good reference to the details of that.
Upvotes: 2