Results 1 to 2 of 2
  1. #1
    atch is offline Member
    Join Date
    Feb 2010
    Posts
    8
    Rep Power
    0

    Thumbs down How to get previous element from collection and correctly synchronize threads?

    Hi,
    I'm trying to do something like this:
    Java Code:
    Vector myRays = new Vector<Ray>();
    if (myRays.size() > 0)
                {
                Iterator<Ray> iter = myRays.iterator();
    
                while(iter.hasNext())
                {
                    if (iter.next().move())
                    {
                     //myRays.remove(myRays.elementAt(myRays.size()-1));//here I'm getting an error
                    }
                }
                }
    I'm getting this error:
    Exception in thread "Thread-5" java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification
    (AbstractList.java:372)

    I suspect that some of my methods should be synchronized (those which has direct access to myRays object) and I've made them synchronized but without any results.
    Thanks for any help.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You cannot modify the list while iterating over it except through the Iterator only.

Similar Threads

  1. how to synchronize a thread
    By sumanandjesus in forum Advanced Java
    Replies: 4
    Last Post: 03-14-2009, 10:26 AM
  2. compare newly added Vector Element with previous elements
    By nidhirastogi in forum Advanced Java
    Replies: 10
    Last Post: 09-10-2008, 01:32 AM
  3. Replies: 0
    Last Post: 04-09-2008, 06:39 PM
  4. How to synchronize threads on an object
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:39 PM
  5. Can't synchronize multiple JPanels in a JFrame
    By vassil_zorev in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2007, 04:22 PM

Tags for this Thread

Posting Permissions

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