Concurrent modification exception

Posted on 2007-08-02
Last Modified: 2013-12-22
Hi Experts,

I am getting the following error messages sometime in the following code.  The code is only executed from one thread.  Documents say that I can still get this exception withing one thread.  How can I fix it?  Other than having the method as synchronized?    The rest of the code gets into a deadlock when I have this method as synchronized?

TnsJavaServer.handleEvent(): Caught exception: java.util.ConcurrentModificationException
Here is the relevant code snippet.

private HashMap<Integer, ArrayList<TnsEventHandlerInt>> m_ViewMonitorMap;

public  void handleEvent( String msg ) {

 try {  
            List eventHandlerList = m_ViewMonitorMap.get(new Integer(rec.getViewId()));
            if(eventHandlerList != null) {
                Iterator it = eventHandlerList.iterator();
                while(it.hasNext()) {
                    TnsEventHandlerInt eventHandler = (TnsEventHandlerInt);
        } catch(Exception e) {
            MiSystem.logError(MiSystem.SYSTEM_SOFTWARE_LOG, getClass(), fName,
                    "Caught exception: " + e.toString());

Question by:ambuli
    LVL 92

    Assisted Solution

    are you sure another thread isn't accessing the list?

    can u post a stack trace for the exception

    LVL 26

    Accepted Solution

    >>The code is only executed from one thread.

    Might be right for this block but the arralists inside the hashmap may be accessed by other threads!

    why don't you iterate through list instance( eventHandlerList ) itself instead of getting the iterator...?

    for e.g.,

    for( TnsEventHandlerInt eventHandler : eventHandlerList ){


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now