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 4 of 4

Thread: how to avoid this concurrent modification exception?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to avoid this concurrent modification exception?

    I have two threads.

    the main thread and the rendering thread.

    the main thread constantly iterates through my entities lists and makes them think and does game logic, etc..

    the rendering thread constantly iterates through the entities list and renders them

    in my main thread, I put in some code that would delete an entity after such and such occurred. however, there is a random chance that this throws a concurrent modification exception with my rendering thread, and I was wondering on what I should do to avoid this?

    I'm using the synchronization keywords, but it's possible that I'm just not using them correctly to fit my situation. My understanding is that two synchronized methods/blocks cannot run concurrently.

            public void RemoveEntity( Entity e )
            {
     
                    synchronized( e )
                    {
     
                            entList.remove( e );
     
                    }
     
            }
     
            public synchronized void Draw( GL10 gl )
            {
     
                    Iterator itr = entList.iterator();
     
                    while( itr.hasNext() )
                    {
     
     
                            Entity e = ( Entity )itr.next();
     
     
     
                                    ( e ).Draw( gl );
     
     
     
                    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to avoid this concurrent modification exception?

    You are synchronizing on different objects. Change the code so you are using one object.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to avoid this concurrent modification exception?

    Quote Originally Posted by Norm View Post
    You are synchronizing on different objects. Change the code so you are using one object.
    Alright, both my render method and my entityremove method are apart of my EntityFactory object which is shared between threads. If I made both functions synchronized, it means that one function cannot run while the other function is occuring, correct?

    So this should fix my problem?

    	public synchronized void RemoveEntity( Entity e )
    	{
     
    		entList.remove( e );
     
     
    	}
     
    	public synchronized void Draw( GL10 gl )
    	{
     
    		Iterator itr = entList.iterator();
     
    		while( itr.hasNext() )
    		{
     
     
    			Entity e = ( Entity )itr.next();
     
     
     
    				( e ).Draw( gl );
     
     
     
    		}
     
     
    	}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to avoid this concurrent modification exception?

    Yes, only one method should execute at a time.

Similar Threads

  1. MODIFICATION OF AN APPLICATION
    By VeliDemir in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: February 8th, 2012, 12:52 PM
  2. Blocking in java.util.concurrent Classes
    By bgroenks96 in forum Threads
    Replies: 0
    Last Post: November 14th, 2011, 10:38 PM
  3. concurrent programming
    By abc in forum Threads
    Replies: 10
    Last Post: June 29th, 2010, 07:06 AM
  4. Java Concurrent Programming
    By vzwsudha in forum Threads
    Replies: 6
    Last Post: May 27th, 2010, 04:26 AM
  5. Replies: 0
    Last Post: May 11th, 2010, 03:37 AM