Ranking

P

Patricia Shanahan

Crouchez said:
Concurrent means multiple threads though doesn't it?

Not necessarily. It just means two things are going on at the same time.
The effective lifetime of an Iterator is from when it is created until
the last call to its methods. A change to the underlying collection
during that period is "concurrent" with the iterator.

Patricia
 
L

Lew

Concurrent means multiple threads though doesn't it?

According to
Note that this exception does not always indicate that an object has been
concurrently modified by a /different/ thread. If a single thread issues a
sequence of method invocations that violates the contract of an object,
the object may throw this exception. For example, if a thread modifies a
collection directly while it is iterating over the collection with a
fail-fast iterator, the iterator will throw this exception.
(emphasis in the original)

Whatever "concurrent" may mean, and whether that meaning actually inherently
implies multithreading or not, the Javadocs explain what the API class
ConcurrentModificationException does.

I tried to construct an SSCCE that demonstrates this and so far have not
succeeded. Regardless, Collections.synchronizedMap() produces no less
synchronized an object than does new Hashtable(). The major differences are
that Hashtable contains non-Collection methods (like elements()) and seemingly
does not alert you to concurrent modifications when you use the associated
Enumeration instead of the more robust Iterator.

I never use Hashtable so I cannot advise you better on its peculiarities.
 
L

Lew

(emphasis in the original)

<sscce>
/* ConcurrentModifier
* $Id$
*/
package testit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/** Cause a <code>ConcurrentModificationException</code>.
*/
public class ConcurrentModifier
{
/** Cause a <code>ConcurrentModificationException</code>.
* @param args <code>String []</code> program arguments.
*/
public static void main( String [] args)
{
List <String> strangs = new ArrayList <String> ();
strangs.add( "first" );
strangs.add( "second" );
strangs.add( "remove" );
strangs.add( "last" );

String val;

Iterator <String> iter = strangs.iterator();
if ( iter.hasNext() )
{
val = iter.next();
System.out.println( val );
System.out.println( "" );
}

if ( iter.hasNext() )
{
if ( strangs.remove( "remove" ) )
{
System.out.println( "Removed" );
}
else
{
System.out.println( "Not removed" );
}
System.out.println( "" );
}

while ( iter.hasNext() )
{
val = iter.next(); // line 50
System.out.println( val );
System.out.println( "" );
}
System.out.println( "out of elements" );
}
}
</sscce>

Output:

first

Removed

Exception in thread "main" java.util.ConcurrentModificationException
at
java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at testit.ConcurrentModifier.main(ConcurrentModifier.java:50)
 
C

Crouchez

Lew said:
(emphasis in the original)

<sscce>
/* ConcurrentModifier
* $Id$
*/
package testit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/** Cause a <code>ConcurrentModificationException</code>.
*/
public class ConcurrentModifier
{
/** Cause a <code>ConcurrentModificationException</code>.
* @param args <code>String []</code> program arguments.
*/
public static void main( String [] args)
{
List <String> strangs = new ArrayList <String> ();
strangs.add( "first" );
strangs.add( "second" );
strangs.add( "remove" );
strangs.add( "last" );

String val;

Iterator <String> iter = strangs.iterator();
if ( iter.hasNext() )
{
val = iter.next();
System.out.println( val );
System.out.println( "" );
}

if ( iter.hasNext() )
{
if ( strangs.remove( "remove" ) )
{
System.out.println( "Removed" );
}
else
{
System.out.println( "Not removed" );
}
System.out.println( "" );
}

while ( iter.hasNext() )
{
val = iter.next(); // line 50
System.out.println( val );
System.out.println( "" );
}
System.out.println( "out of elements" );
}
}
</sscce>

Output:

first

Removed

Exception in thread "main" java.util.ConcurrentModificationException
at
java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at testit.ConcurrentModifier.main(ConcurrentModifier.java:50)

That's it - it wasn't a different thread - items were being removed inside
the iterator.. doh

What's the best way to do remove while in the loop? Is it to store the
objects temporarily in an array and remove after or is there a built-in api
way?
 
L

Lew

Crouchez said:
That's it - it wasn't a different thread - items were being removed inside
the iterator.. doh

What's the best way to do remove while in the loop? Is it to store the
objects temporarily in an array and remove after or is there a built-in api
way?

Iterator.remove().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top