I want to properly close Closeable object when it’s no longer referenced by other threads.
I wrote some small test, but after object is enqueued the get method return null, i.e. the poll method returns proper Object which has no referent.
public static void main(String[] args)
{
ReferenceQueue<Closeable> reaped = new ReferenceQueue<Closeable>();
Closeable s = <SOME CLOSEABLE IMPL>;
WeakReference<Closeable> ws = new WeakReference<Closeable>(s, reaped);
s = null;
System.gc();
Closeable ro = (Closeable)reaped.poll().get();
ro.close();
}
Thanks in advance.
Any help will be appreciated.
First, if it is only about closing, use
PhantomReference.Next, from the reference queue,
poll()does not guarantee that you will get the reference back. and you will never get the actual object (referent) back.If you want to make sure your
Closeables are closed you have to keep track of them yourself lets say in aMap<Reference<?>, Closeable>. Then when youpoll()your reference queue, you will eventually get therefthen you have to use it to get theCloseablefrom the map.Note If you don’t have a real reason to do this or if you do not understand what are you really doing, DO NOT do this kind of thing.
You can close your files in
finallyand BTW if it is about files, sockets, etc, they are closed for you (they already implementfinalize()