Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  foreach and concurrent modifcation  (Read 2986 times)
0 Members and 1 Guest are viewing this topic.
Offline aldacron

Senior Member


Medals: 9
Exp: 16 years


Java games rock!


« Posted 2004-04-14 03:20:36 »

I've not seen this documented anywhere, though admittedly I've read little about the intimate details of 1.5 features. When using the new foreach syntax on a collection, you'll run into trouble with code like this:


for(SomeObject o : someCollection)
{
   if(!o.someCondition())
       someCollection.remove(o);
}


The above will throw a ConcurrentModificationException. I suppose that's because the foreach is working with an Iterator behind the scenes, which of course isn't happy that you tried to remove the object directly.

I don't see this as being a bug, as that's how iterators are supposed to behave - but it surely is annoying. It cost me about 30 minutes today trying to figure out how the hell I was concurrently modifying a list from a single thread. So whenever you need to conditionally remove an item from collection in a loop like that, use an iterator rather than the
foreach. And of course, use clear() to empty the whole thing at once after a loop.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2004-04-14 10:01:58 »

Presumably there is some way to access the underlying iterator's .remove() method?

malloc will be first against the wall when the revolution comes...
Offline rreyelts

Junior Member




There is nothing Nu under the sun


« Reply #2 - Posted 2004-04-14 22:40:30 »

Quote
Presumably there is some way to access the underlying iterator's .remove() method?

No.

God bless,
-Toby Reyelts

About me: http://jroller.com/page/rreyelts
Jace - Easier JNI: http://jace.reyelts.com/jace
Retroweaver - Compile on JDK1.5, and deploy on 1.4: http://retroweaver.sf.net.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 363
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2004-04-15 07:36:58 »

So the whole feature is about as useful as a chocolate fireguard then. As expected.

Cas Smiley

Offline rreyelts

Junior Member




There is nothing Nu under the sun


« Reply #4 - Posted 2004-04-15 15:20:09 »

Quote
So the whole feature is about as useful as a chocolate fireguard then. As expected.

I wouldn't say it is useless, but it is very light syntactic sugar. It might be nice if they extended the syntax so that you could specify the name of the iterator, too. Something like:

1  
2  
3  
// declares an Iterator<Foo> fooIt, and assigns it the value of foos.iterator();
for ( Foo f : fooIt : foos ) {
}

God bless,
-Toby Reyelts

About me: http://jroller.com/page/rreyelts
Jace - Easier JNI: http://jace.reyelts.com/jace
Retroweaver - Compile on JDK1.5, and deploy on 1.4: http://retroweaver.sf.net.
Offline puf

Senior Newbie




Java games rock!


« Reply #5 - Posted 2004-08-11 05:28:06 »

I was just thinking about this a few days ago. The easiest way to solve the problem is to add the objects to a secondary collection during the forEach and then remove them all in one go after the loop.

So it should be something like this:

Collection<SomeObject> removableObjects = new Collection<SomeObject>();
for (SomeObject o: someCollection)
{
 if (!o.someCondition())
   removableObjects.add(o);
}
someCollection.removeAll(removableObjects);

It actually allows the JVM to more efficiently remove the objects all in one go, although I doubt whether it will make much difference at the moment.

            Puf
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #6 - Posted 2004-08-11 06:21:20 »

Quote
It actually allows the JVM to more efficiently remove the objects all in one go, although I doubt whether it will make much difference at the moment.

            Puf


Isn't that highly dependent upon data structure, though?

For instance, isn't it much better to iterate over a huge linked list only once, removing as you go, than to iterate over it twice, but with a reduced number of reference re-assignments (assuming best-case scenario, that many removed items are contiguous)?

If the items are discontiguous, so that exactly the same number of ops are needed for the removes in each case, then the double iteration is always going to be much slower.

Bascially, it's shockingly short-sighted syntactic sugar. Sigh. /me wonders what happened in the java decision process to let *this* through

malloc will be first against the wall when the revolution comes...
Offline puf

Senior Newbie




Java games rock!


« Reply #7 - Posted 2004-08-11 06:36:17 »

You're right, it is HIGHLY dependant on the exact implementation.  I probably shouldn't have made a claim that far fetched. Sorry about that.

But I happen to like the syntactic sugar that the forEach is offering, so I'm looking for workarounds for the problems. Since I've had similar problems with standard Iterators, I was looking for a solution that works in all cases. Having a second collection of objects should always work, although not at the best possible speed.

Given the available development time saved versus the deployment time lost, it might be worth using in some cases.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #8 - Posted 2004-08-13 02:30:02 »

The foreach thing is nice for when you don't need to modify the collection as you go.  It's as simple as that. If you need to modify the collection then simply use the iterator as you would with 1.4.... but you can use Generics this time to make it a little more typesafe.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!