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.