Iterating Collection In Two Threads

Posted by ChloeRadshaw on Stack Overflow See other posts from Stack Overflow or by ChloeRadshaw
Published on 2010-04-11T07:57:48Z Indexed on 2010/04/11 8:03 UTC
Read the original article Hit count: 290

Filed under:
|

This question relates both to C# and Java

If you have a collection which is not modified and that collection reference is shared between two threads what happens when you iterate on each thread??

ThreadA: Collection.iterator ThreadA: Collection.moveNext ThreadB: Collection.iterator ThreadB: Collection.moveNext

Will threadB see the first element?

Is the iterator always reset when it is requested? What happens if this is interleaved so movenext and item is interleaved? Is there a danger that you dont process all elements??

© Stack Overflow or respective owner

Related posts about c#

Related posts about java