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 8192037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:04:21+00:00 2026-06-07T04:04:21+00:00

The code I am working on is throwing the aforementioned exception. I am not

  • 0

The code I am working on is throwing the aforementioned exception. I am not very experienced with multi-threaded programming and I’m not having a lot of luck troubleshooting this.

The program is written in Java using Processing and OSC. The main OSC event handler is adding elements to a Vector. It is triggered on user input and therefore highly unpredictable. This Vector is also being iterated over and updated in Processing’s animation thread which happens very regularly at about 60 times per second.

Occasionally, the OSC events handler is called as the Vector is being iterated over in the animation thread and the exception is thrown.

I have tried adding the “synchronized” modifier to the OSC event handler. I have also attempted to cue changes to the Vector until the next frame ( time step ) of the animation thread, but I’m finding that it just ends up delaying the exception being thrown.

What can I do to prevent this behavior? Is there a way to only access to the Vector if it isn’t already in use?

Update:
Two answers have suggested that the list is having elements added or removed as it is being iterated over. This is in fact what is happening due to the fact that OSC is triggering the handler from a thread other than the thread that is iterating over the list. I am looking for a way to prevent this.

Here is some pseudo-code:

Vector<String> list = new Vector<String>();
Vector<Particle> completedParticles = new Vector<Particle>();

public void oscEvent( OSCMessage message )
{
    list.add( new Particle( message.x, message.y ) );
}

public void draw()
{
    completedParticles.clear();
    for( Particle p : list )
    {
        p.draw();
        if( p.isComplete ) {
            completedParticles.add( p );
        }   
    }
    list.removeAll( completedParticles );
}
  • 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-07T04:04:22+00:00Added an answer on June 7, 2026 at 4:04 am

    About your Code

    In your code, your for-each loop is iterating over the list, and your osEvent modifies the list. Two threads, running simultaneously could be trying to: iterate over the list while other is adding elements to it. Your for loop creates an iterator.

    You can do the following (provided that these are the only two places where this occurs):

    //osEvent
    synchronized(this.list) {
       list.add( new Particle( message.x, message.y ) );
    }
    
    //draw
    synchronized(this.list) {
      for( Particle p : list )
        {
            p.draw();
            if( p.isComplete ) {
                completedParticles.add( p );
            }   
        }
    }
    

    Or, as I explain below, make a copy of the vector before iterating over it, that will probably be better.

    About Concurrent Modification Exception

    This exception is not necessarily thrown in a multithreaded code. It happens when you modify a collection while it is being iterated. You can get this exception even in single-threaded applications. For instance, in a for-each loop, if you remove or add elements to a list, you end up getting a ConcurrentModificationException.

    As such, adding synchronization to the code will not necessarily solve the problem. Some alternatives consist in making a copy of the data to be iterated, or using iterators that accept modifications (i.e. ListIterator), or a collection with snapshot iterators.

    Evidently, in a multithreaded piece of code, you would still have to take care of synchronization to avoid further problems.

    Let me give some examples:

    Let’s say you want to delete items from a collection while iterating over it. Your alternatives to avoid a ConcurrentModificationException are:

    List<Book> books = new ArrayList<Book>();
    books.add(new Book(new ISBN("0-201-63361-2")));
    books.add(new Book(new ISBN("0-201-63361-3")));
    books.add(new Book(new ISBN("0-201-63361-4")));
    

    Collect all the records that you want to delete within an enhanced for loop, and after you finish iterating, you remove all found records.

    ISBN isbn = new ISBN("0-201-63361-2");
    List<Book> found = new ArrayList<Book>();
    for(Book book : books){
        if(book.getIsbn().equals(isbn)){
            found.add(book);
        }
    }
    books.removeAll(found);
    

    Or you may use a ListIterator which has support for a remove/add method during the iteration itself.

    ListIterator<Book> iter = books.listIterator();
    while(iter.hasNext()){
        if(iter.next().getIsbn().equals(isbn)){
            iter.remove();
        }
    }
    

    In a multithreaded environment, you might consider making a copy of the collection before iterating, as such, allowing others to modify the original collection without affecting iteration:

    synchronized(this.books) {
       List<Book> copyOfBooks = new ArrayList<Book>(this.books)
    }
    for(Book book : copyOfBooks) {
       System.out.println(book);
    }
    

    Alternatively, you may consider using other types of collections using snapshot iterators, like java.util.ConcurrentCopyOnWriteArrayList which guarantees not to throw ConcurrentModificationException. But read the documentation first, because this type of collection is not suitable for all scenarios.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code that was working fine last week is suddenly throwing this exception: System.Data.SqlClient.SqlException (0x80131904):
Why the code is Not working for me, Even the connection is stale, (there
A C++ project I'm working on terminates upon throwing a first-chance exception. This occurs
Possible Duplicate: setOnClickListener not working and throwing error I am trying to develop a
I'm working with some code (not mine I hasten to add, I don't trust
I was working with a base64 encoding script, but it is throwing a lot
I'm working on someone else's code, and it's throwing an error that 'InputStreamError' is
Everything was working fine, and then our code starts throwing: Cannot create ActiveX component
I have this code working in C#: var request = (HttpWebRequest)WebRequest.Create(https://x.com/service); request.Method = GET;
Why isn't this code working? I've been stuck on this for 2 days. public

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.