Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 16. Collection Patterns > Collection Concurrency

16.11. Collection Concurrency

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.

16.11.1. Enumerate a Copy of the Collection

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.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial