Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:39:25+00:00 2026-06-13T18:39:25+00:00

I made some modifications on my multithread program and started to get ConcurrentModificationException s

  • 0

I made some modifications on my multithread program and started to get ConcurrentModificationException s at an object’s HashTable, so I made all accesses to this HashTable into synchronised methods to avoid concurrent accesses to my object, unlike my expectation it didn’t solved the problem. Here is a outline of the design used:

First I’ve got a JTable that displays some data of an usb device, this JTable asks for a communication-core object that implements Observer (the one that reads and writes data to my device) through an Observable object the data it wants, the data it wants depends of which rows are displayed so it’s updated when the user scrolls de table, than this communication-core object gets this notification through synchronized update method that calls other synchronized method that updates a HashTable which saves the last read values for after use (this communication class notifies back my JTable class and other objects when it reads values though other Observable object). The exception is occuring in this last method while updates the HashTable. Here is a simplification of the code:

The following methods are of the communication-core object

public synchronized void update(Observable o, Object arg)
{
    // do some other work
    // calls the second synchronized method
    updateMonitorList();
}

private synchronized void updateMonitorList()
{
    // updates a list of monitoring addresses (no problem here)
    // m_lastValues is the HashTable that is giving me headaches
    Iterator<Parameter> itr = m_lastValues.keySet().iterator();
    while (itr.hasNext())
    {
        // removes the currently not used parameters from the HashTable
        Parameter p = itr.next(); // concurrent exception here WHY? :/
        boolean contains = false;
        for (int i = 0; i < m_monitorList.size(); i++)
        {
            if (p.equals(m_monitorList.get(i)))
            {
                contains = true;
                break;
            }
        }
        if (!contains)
        {
            m_lastValues.remove(p);
        }
    }
    // more operations with the HashTable
    for (int i = 0; i < m_monitorList.size(); i++)
    {
        // adds newly added parameters to the hashtable
        boolean contains = false;
        for (Observer key : m_requestedParameters.keySet())
        {
            if (key.equals(m_monitorList.get(i)))
            {
                contains = true;
                break;
            }
        }
        if (!contains)
        {
            m_lastValues.put(m_monitorList.getAt(i), m_monitorList.getAt(i).m_intUserSetting);
        }
    }
}

// this method is used to know if a value has changed
private synchronized boolean getParameterChanged(Parameter currentParamer)
{
    Integer v = m_lastValues.get(currentParamer);
    return v == null || v != currentParamer.m_intUserSetting;
}

I need this approach because I have multiple windows that asks for values from the usb device, and this communication object takes care of it, there was no problem with concurrency before I added this HashTable of last values. This m_lastValues HashTable is not used in any other method not listed above.

Any suggestions? Thanks in advance.

[ EDIT ]

Actually that was not a multithread problem, it was just a misunderstood exception meaning. As @Tudor pointed out the problem was the remove() method inside the while loop. Here is how I solved it:

This code goes into updateMonitorList() method, note that no Iterator was needed (the first question was with a for loop like that), actually the Iterator made no difference:

    ArrayList<Parameter> remove = new ArrayList<Parameter>();
    for (Parameter p : m_lastValues.keySet())
    {
        boolean contains = false;
        for (int i = 0; i < m_monitorList.size(); i++)
        {
            if (p.equals(m_monitorList.get(i)))
            {
                contains = true;
                break;
            }
        }
        if (!contains)
        {
            remove.add(p);
        }
    }
    for (int i = 0; i < remove.size(); i++)
    {
        m_lastValues.remove(remove.get(i));
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:39:27+00:00Added an answer on June 13, 2026 at 6:39 pm
    for (Parameter key : m_lastValues.keySet()) // exception this line
    {
        // removes the currently not used parameters from the HashTable
    }
    

    The problem is here. You can’t change the collection while iterating over it, regardless of synchronization. It is a single-threaded issue actually.

    What you can do is either switch to an explicit iterator and use remove() or save the items to remove in a separate collection and remove them afterwards.

    Iterator<Parameter> iter = m_lastValues.keySet().iterator();
    while (iter.hasNext()) {
        Parameter current = iter.next();
        for (int i = 0; i < m_monitorList.size(); i++) {
            if(current.equals(m_monitorList.get(i)) {
                iter.remove();
                break;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I copied a tutorial program and made some modifications and now when i run
I have already asked some questions about this topic, I have made some modifications
I made some modifications to SVG Edit which contains a make file. When I
I made some ajax sites in the past where I used ajax to get
It made some errors like this: KDnsCache.cpp: In member function ‘unsigned int KDnsCache::GetName(const char*)’:
it had a existing git checkout and had made some modifications, then i checked
I realized that in a old commit I made some modifications to a file
I have made some modifications to nsMediaStream.h/cpp in the Mozilla (6.0.2) code and one
EDIT Thank you all for your assistance. I have made the modifications to the
I have made some modifications to FullCalendar. The gist of the mods are I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.