Synchronized Collections
In Chapter 15, we discussed multithreading. Most of the non-generic collections are unsynchronized by default, so they can operate efficiently when multithreading is not required. Because they are unsynchronized, however, concurrent access to a collection by multiple threads could cause errors. To prevent potential threading problems, synchronization wrappers are used for many of the collections that might be accessed by multiple threads. A wrapper object receives method calls, adds thread synchronization (to prevent concurrent access to the collection) and passes the calls to the wrapped collection object. Most of the non-generic collection classes in the .NET Framework provide static method Synchronized, which returns a synchronized wrapping object for the specified object. For example, the following code creates a synchronized ArrayList:
ArrayList notSafeList = new ArrayList(); ArrayList threadSafeList = ArrayList.Synchronized( notSafeList );
The collections in the .NET Framework do not all provide wrappers for safe performance under multiple threads. Some guarantee no thread-safety at all. Many of the generic collections are inherently thread-safe for reading, but not for writing. To determine if a particular class is thread-safe, check that class's documentation in the .NET Framework class library reference.
Also recall that when a collection is modified, any enumerator returned previously by the GetEnumerator method becomes invalid and will throw an exception if its methods are invoked. Because other threads may change the collection, using an enumerator is not thread-safethus, the foreach statement is not thread-safe either. If you use an enumerator or foreach statement in a multithreaded application, you should use the lock keyword to prevent other threads from using the collection or use a TRy statement to catch the InvalidOperationException.