At 6:39 PM +0200 10/4/05, Olivier Jaquemet wrote:
>In every case I think I will use this to prevent any problem but why nobody
>uses finalize methods? is it somehow bad to try to close things correctly that
>way?
Because they are not run under "brutal termination" conditions. For that you
need to add a shutdown hook to the Java runtime. A shutdown hook will run if
the runtime process receives a signal eg. SIGHUP which on Unix occurs if the
controlling terminal "hangs up", etc. It won't get run on a SIGKILL, but
nothing will. It also runs if you get an OutOfMemoryException or similar
problem in the runtime, though there is no guarantee the hook code e.g. close()
won't also fail in that case.
For example, my indexing code looks something like this:
final IndexWriter writer = ...
Thread shutdown = new Thread() {
public void run() {
if(writer != null) {
// feebly try to prevent concurrent reentry problems
IndexWriter w = writer;
w = null;
try {
w.close();
} catch(Exception ex) {
logger.error("Closing", ex);
}
}
}
};
Runtime.getRuntime().addShutdownHook(shutdown);
// do indexing here
doit(writer, ...);
// no abnormal termination
// just exit and the shutdown hook will run
In a multi-threaded context, one might need to add synchronization to prevent
concurrent entry from causing problems.
I'm not sure whether this relates to the IndexSearcher problem you're trying to
solve, but it is certainly useful when indexing.
- J.J.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]