Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ConcurrentModificationException - Tried Synchronizing, ListIterator, etc.

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default ConcurrentModificationException - Tried Synchronizing, ListIterator, etc.

    Hello all,

    I rarely come across a Java problem I can't figure out myself but this one's got me stumped. Basically I have a program that needs to run an update everyday. In order to maximize efficiency I made this multi-threaded and put a cap on the number of concurrent threads that can run. So the update function basically creates a List<Thread> of threads and then creates another list of Active Threads (also a List<Thread>). Both of which are ArrayLists. Then it runs through a while loop (below) and starts threads when the number of active threads is below a certain number and then scans the list to remove any threads which are finished. The threads themselves have a few boolean statuses (started, finished, etc.) and synchronized functions to access those variables. I keep getting ConcurrentModificationExceptions (the line is noted in the below code) I imagine because the UpdateThread objects are changing themselves (changing some boolean variables when they're finished). I tried throwing a synchronized block around the loop code (even though I feared that would hurt the performance), I tried a ListIterator, etc. but it still didn't work. The UpdateThread objects themselves just connect to a website, download some data, parse it and then upload it to a local database, but I don't think that's what's causing the issues, just FYI.

    If anyone has any ideas I'd greatly appreciate it, the sooner I get this working the better, it's the last component of the program.

            //Execute the threads, removed completed ones, until all threads are done
            List<UpdateThread> activeThreads = new ArrayList<UpdateThread>();
            while(!threads.isEmpty() || !activeThreads.isEmpty())
            {
                //Check if we can add more threads
                while(activeThreads.size() < UpdateThread.MAX_CONCURRENT_THREADS && threads.size() > 0)
                {
                    //Add next thread to active threads until full
                    activeThreads.add(threads.remove(0));
                }
                //Update the active threads list, start any unstarted, remove any finished
                for(UpdateThread ut : activeThreads)   // *** ERROR is thrown HERE
                {
                    if(ut.isFinished())
                    {
                        //Remove any finished
                        if(ut.success())
                        {
                            updatedSuccessfully++;
                        }
                        activeThreads.remove(ut);
                        updated++;
                    }
                    else if(ut.isRunning() == false)
                    {
                        //Start any unstarted
                        ut.start();
                    }
                    else{
                        //Do nothing, running but not done
     
                    }
                }
                //Sleep
                try{
                    Thread.sleep(UpdateThread.UPDATE_THREAD_CHECK_SLEEP);
                }catch(Exception e)
                {
                    //Do Nothing
                }
            }

    Thanks!

    --- Update ---

    Figured it out, I'm an idiot. Just had to remove it using the iterator:

                //Update the active threads list, start any unstarted, remove any finished
                Iterator<UpdateThread> iter = activeThreads.iterator();
                while(iter.hasNext())
                {
                    UpdateThread ut = iter.next();
                    if(ut.isFinished())
                    {
                        //Remove any finished
                        if(ut.success())
                        {
                            updatedSuccessfully++;
                        }
                        iter.remove();
                        updated++;
                    }
                    else if(ut.isRunning() == false)
                    {
                        //Start any unstarted
                        ut.start();
                    }
                    else{
                        //Do nothing, running but not done
     
                    }
                }
                //Sleep
                try{
                    Thread.sleep(UpdateThread.UPDATE_THREAD_CHECK_SLEEP);
                }catch(Exception e)
                {
                    //Do Nothing
                }

    In case anyone else has this issue, here you go. Works like a charm.
    Last edited by DougD720; May 9th, 2013 at 10:39 PM. Reason: Figured it out.

  2. The Following User Says Thank You to DougD720 For This Useful Post:

    jps (May 10th, 2013)


  3. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: ConcurrentModificationException - Tried Synchronizing, ListIterator, etc.

    Thank you for posting back with the solution to the problem you were having.

Similar Threads

  1. Replies: 1
    Last Post: April 17th, 2012, 06:21 AM
  2. Help with ListIterator
    By vluong in forum Collections and Generics
    Replies: 5
    Last Post: October 16th, 2009, 09:33 AM

Tags for this Thread