Close a FileHandle if WeakReference is garbage collected

93 views Asked by At

I have a class of the following structure:

public class RandomMatrixIndex implements IIndex {

    private final HashMap<Long, Long> index;
    private RandomAccessFile rand;

    // constructor and methods.
}

In the constructor the RandomAccessFile rand is opened. In another class i created a hashmap:

 private final HashMap<String, WeakHashMap<String, IIndex>> _MATRIX_MAP;
 _MATRIX_MAP.put(....);

The map contains a mapping to a IIndex interface which is implemented by the class above. If I put the RandomMatrixIndex in the map, the reference could be garbage collected and I cant close the RandomAccessFile properly.

Now my question. How do i close the RandomAccessFile if the reference to the WeakHashMap is lost? DO I have to include a finalizer in the RandomMatrixIndex constructor? OR is the filehandle closed automatically. (pls forgive if this is poor coding style)

Thanx alot!

0

There are 0 answers