commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Muller <...@online.no>
Subject Re: [JCS] ConcurrentModificationException
Date Wed, 25 Jan 2012 12:26:47 GMT
Hi,

I don't know JCS at all, but the exception you get is the result of one 
thread updating a collection (in your case a HashMap) whilst another 
thread is iterating it. This would happen even if the Map was decorated 
using the Collections.synchronizedMap( myMap ) facility. To allow /true/ 
concurrent access/updates to a Map, the 
java.util.concurrent.ConcurrentHashMap (or another class implementing 
the ConcurrentMap) must be deployed.

If you use a synchronized Map as underlying structure for something, 
then you have to protect an iteration using the map itself as a 
synchronization token.

public class Users {

    private final Map< String, User > users = 
Collections.synchronizedMap( new HashMap< String, User >() );

    public void insertUser( User u ) {
       Require.notNull( u );
       users.put( u.getName(), u);
    }

    public Collection< User > findUsersBornBefore( Date date ) {
        List< User > result = new ArrayList< User >();
        synchronized( users ) { // if not synchronized, the users 
structure may be concurrently modified
            for( User user : users.values() ) {
                if( user.getBirthdate().before( date ) ) {
                     result.add( user );
                 }
            }
        }
        return result;
    }

}

May there is a way you can force JCS to use another Map implementation 
than HashMap? Some factory method somewhere?

-- 

Thomas

On 25/01/2012 12:03, NM, Vijayakumar wrote:
> Hi All,
>
> We have an application which uses JCS cache to write data into IndexedDiskCache, sometime
are getting ConcurrentModificationException when application tries to update the cache object
frequently, please let me know what could be the reason and is there any workarounds to solve
the issue. We are using JCS 1.3
>
> 2012-01-25 10:38:44,088 ERROR [RemoteCache] -<Disabling remote cache due to error:
Failed to put [TEMP1] to tempRegion>
> java.util.ConcurrentModificationException
>                  at java.util.HashMap$HashIterator.nextEntry(HashMap.java:841)
>                  at java.util.HashMap$EntryIterator.next(HashMap.java:883)
>                  at java.util.HashMap$EntryIterator.next(HashMap.java:881)
>                  at java.util.HashMap.writeObject(HashMap.java:1037)
>                  at sun.reflect.GeneratedMethodAccessor15.invoke(Unknown Source)
>                  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>                  at java.lang.reflect.Method.invoke(Method.java:592)
>                  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:917)
>                  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1339)
>                  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
>                  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
>                  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)
>                  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)
>                  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
>                  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
>                  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302)
>                  at org.apache.jcs.utils.serialization.StandardSerializer.serialize(StandardSerializer.java:50)
>                  at org.apache.jcs.utils.serialization.SerializationConversionUtil.getSerializedCacheElement(SerializationConversionUtil.java:73)
>                  at org.apache.jcs.auxiliary.remote.RemoteCache.update(RemoteCache.java:207)
>                  at org.apache.jcs.engine.CacheAdaptor.handlePut(CacheAdaptor.java:91)
>                  at org.apache.jcs.engine.PooledCacheEventQueue$PutEvent.doRun(PooledCacheEventQueue.java:474)
>                  at org.apache.jcs.engine.PooledCacheEventQueue$AbstractCacheEvent.run(PooledCacheEventQueue.java:397)
>                  at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:748)
>                  at java.lang.Thread.run(Thread.java:595)
>
> Regards,
> Vijay
>
>
>
>
>
>
>
> This message contains information that may be privileged or confidential and is the property
of the Capgemini Group. It is
> intended only for the person to whom it is addressed. If you are not the intended recipient,
you are not authorized to
> read, print, retain, copy, disseminate, distribute, or use this message or any part thereof.
If you receive this message
> in error, please notify the sender immediately and delete all copies of this message.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message