Results 1 to 7 of 7
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default I'm getting a ConcurrentModificationException

    Hello,

    I'm having trouble with a ConcurrentModificationException. I've got a LinkedList with more than one ListIterator tracking it. For some reason, if I call the next() method on one, and then do the same for another, it throws a ConcurrentModificationException. I did a bit of research on this and I found that it happens when an iterator is trying to traverse the list while something else is trying to modify it. It's strange because I'm sure nothing is modifying the list, but maybe it thinks that if another iterator is trying to traverse the list that counts as a modification. But in any case, one solution I came across was to synchronize the call to next(). I tried this but I'm not having any luck. Maybe it's because I don't know how to used synchronization, but here's what I've tried:

    Java Code:
    synchronized (pointList) {
        p = (Point) iterator.next();
    }
    I've also tried this:

    Java Code:
    synchronized (this) {
        p = (Point) iterator.next();
    }
    Needless to say, the call to next() is not made within pointList. I'm not sure what goes into the brackets of synchronized, but I'm sure it has to be whatever needs protection from modification while the call to next() is made, so I initially tried pointList.

    Anyway, it isn't working. I'm still getting the ConcurrentModificationException.

    Any help would be much appreciated.
    Last edited by gib65; 08-17-2010 at 10:51 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Look at the Collections class. It has wrapper classes that:
    Returns a synchronized (thread-safe) collection backed by the specified collection.

  3. #3
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Hhm...

    I found what I think would work best:

    List<E> Collections.synchronizedList(List<E>)

    and I'm trying to initiate my LinkedList with it. I'm doing this:

    pointList = Collections.synchronizedList(new LinkedList<Point>());

    but the compiler tells me:

    incompatible types

    and refers to the fact that I'm trying to assign a List object to a LinkedList. Unfortunately, I get a runtime error if I cast it:

    pointList = (LinkedList<Point>) Collections.synchronizedList(new LinkedList<Point>());

    I get a ClassCastException.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Collections.synchronizedList() returns a List not a LinkedList

    What is the type of pointList?
    Do you need a LinkedList? Will a List do what you need?

  5. #5
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    The type is java.awt.Point.

    List appears to be an interface. Would that not imply that I have to create a class that implements it and then flesh out all the methods (and there are a lot)? Even if I were to do this, I'm not sure what I would program into them as the API on Lists doesn't specify what data structures are involved in maintaining and manipulating the List. For all that effort, I might as well create my own class (or at least, create my own iterator - which I'm thinking of doing).

    In any case, I tried creating a List and a got a few compiler errors. First I tried this:

    List<Point> list = new List<Point>();

    to which I got the following compiler error:

    type java.awt.List does not take parameters

    referring to the <Point> parameter.

    Then I removed <Point>:

    List list = new List();

    and the compiler told me:

    cannot find symbol
    symbol: method add(java.awt.Point)

    referring to this:

    list.add(new Point(50, 100));

    So I'm at a loss with Lists.

    It's OK if there's no easy fix to this. I'm thinking I'm just going to use a simple int as my iterator (it'll store the index of the next item in the list) and manage it myself (which shouldn't consist of anything more complicated than incrementing and decrementing it as needed).

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gib65 View Post
    List<Point> list = new List<Point>();

    to which I got the following compiler error:

    type java.awt.List does not take parameters
    As you already noticed, List is an interface so you can't instantiate it; instantiate a class that implements that interface such as a LinkedList or an ArrayList.

    kind regards,

    Jos

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    I thought that you had an existing Linked list that you wanted to interate thru on two different threads without getting the exception. If you put that linked list into a synchronizedList for each thread to iterate thru, the doc implies that you won't get the error.
    The List interface provides an accessor method that returns a ListIterator.

Similar Threads

  1. Getting around a ConcurrentModificationException
    By Newbie666 in forum New To Java
    Replies: 50
    Last Post: 01-26-2010, 02:30 PM
  2. Replies: 19
    Last Post: 08-28-2009, 12:49 AM
  3. ConcurrentModificationException with ArrayList
    By fogus in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 04:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •