JVM provides you a hook to register a thread with the shutdown initiation sequence. Once a thread is registered, on every shutdown that thread is run.
Now, is there any such a hook java provide to register a thread with JVM's Garbage collector?
JVM provides you a hook to register a thread with the shutdown initiation sequence. Once a thread is registered, on every shutdown that thread is run.
Now, is there any such a hook java provide to register a thread with JVM's Garbage collector?
Not exactly a hook, but you can use a WeakReference to be notified that an object has become eligible to be garbage collected.
There are WeakReferences as already mentioned but there are also Phantom References
All of these techniques just allow you to monitor garbage collection on specific objects though.
There is a good description of phantom references here: http://java.dzone.com/articles/finalization-and-phantom
You do have finalizers, but they're not guaranteed to run.
Further discussion about why finalizers may not run in this thread: When is the finalize() method called in Java?