You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Stephen Colebourne <sc...@btopenworld.com> on 2002/11/24 18:34:01 UTC

Re: [Collections] SoftRefHashMap.purge throws ConcurrentModificationException

I have applied the suggested fix, as it appears to be sensible, thanks.

However, we now advise using ReferenceMap instead of SoftRefHashMap, as the
latter is severly broken.

Stephen

----- Original Message -----
From: "Eduardo Francos" <ef...@incontext.fr>
> I'm using the SoftRefHashMap to manage images and I get a
> ConcurrentModificationException when I call the purge method.
> Looking at the code I found that the method removes GCed references
> directly from the Map instead of using the iterator's remove method.
> I subclassed SoftRefHashMap and implemented the following change in
> purge that works.
>
> public void purge()
> {
>     Map map = getMap();
>     Set keys = map.keySet();
>     if (keys == null) {
>         return;
>     }
>     for (Iterator i = keys.iterator(); i.hasNext(); ) {
>         Object key = (Object)i.next();
>         Reference ref = (Reference)map.get(key);
>         if (ref.get() == null) {
>             // Fix for ConcurrentModificationException
>             // Previously:
>             // map.remove( key );
>             i.remove();
>             // end ConcurrentModificationException fix
>         }
>     }
> }
>
> Either I was doing something wrong or the fix is required.
>
> Eduardo
>
>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [Collections] SoftRefHashMap.purge throws ConcurrentModificationException

Posted by Eduardo Francos <ef...@incontext.fr>.
In article <00...@oemcomputer>, Stephen Colebourne wrote:
> I have applied the suggested fix, as it appears to be sensible, thanks.
> 
> However, we now advise using ReferenceMap instead of SoftRefHashMap, as the
> latter is severly broken.
> 
> Stephen
> 

Thanks, I got the new 2.1 version and am using ReferenceMap instead.
I noticed that the purge method as become private and is called in many places in the source code, but 
the documentation for the method states that it is called only for write operations.
I must be sure that the stale mappings are removed for read operations too (ex:size) and though 
currently the implementation calls purge() for all read methods as well can this change in the future?

Thanks
Eduardo





--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>