Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Collections should not be modified during an enumeration or modified concurrently from another thread. This section describes a couple of techniques for avoiding changes in a collection until after an enumeration is complete. After that, the thread-safety issues are addressed.
The first technique to avoid modifying a collection while it is being iterated is to simply copy the collection and iterate through the copy, leaving the original free to be altered. The code in Listing 16-14 demonstrates this technique.