commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jack, Paul" <>
Subject RE: [Collections][SUBMIT] ReferenceMap. Take FOUR.
Date Thu, 01 Aug 2002 17:07:27 GMT
> >Actually, that was my initial understanding as well.  The
> >"take three" version of ReferenceMap only purged during 
> >mutations.  But after reviewing the source for 
> >java.util.WeakHashMap, I discovered that it in fact clears 
> >out its ReferenceQueue on all operations, even accessors 
> >(even simple ones like isEmpty()).  I'm looking at the 1.4
> >source, I'm not sure if previous versions of WeakHashMap 
> >behaved the same way.
> I was looking at JDK 1.3 source. WeakHashMap clears the ref 
> queue only in mutators in 1.3. Perhaps it's worthwhile to do a 
> diff to see why/what/how Sun changes WeakHashMap.

The implementation of WeakHashMap is radically different in 1.3
vs 1.4.

In 1.3, it's implemented using an internal HashMap.  The approach
is surprisingly similar to SoftRefHashMap's, except they use an
efficient ReferenceQueue instead of iterating over the whole
map on each purge.

In 1.3, a purge couldn't happen on read operations, because that
would have generated ConcurrentModificationExceptions during the

In 1.4, they completely re-wrote WeakHashMap so that it duplicates
many of the algorithms found in HashMap, rather than using an
internal HashMap.  This seems to be faster for many operations,
and also allowed them to safely purge before any and all 
operations, even read operations.

So it would seem best to mimic 1.4's WeakHashMap, which is what
ReferenceMap take four does.

However, upon re-re-reviewing the code for 1.4 WeakHashMap, I 
realized that they aren't incrementing the modCount during a 
purge operation.  (That's why a read operation won't raise a
ConcurrentModificationException during iteration).  Because of
the way the iterator is coded, this is always perfectly safe
(it never leads to inconsistency in the structure of the map).
Since ReferenceMap's iterator borrows heavily from 1.4 
WeakHashMap's, I think I can safely remove the modCount++ from

So there will have to be a ReferenceMap, Take 5.  



To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message