Why is List <T> not thread safe?

From the following site:

http://crfdesign.net/programming/top-10-differences-between-java-and-c

Unfortunately, List<> not thread safe (C # s ArrayList and Javas Vector are thread safe). C # also has a Hashtable ; general version:

What makes List<T> not thread safe? Is this a problem with implementing part of the .NET Framework? Or are generics not thread safe?

+42
generics thread-safety
Apr 01 '09 at 3:49
source share
6 answers

You really need to classify the Java type of thread safety. Javas Vector is safe to use from multiple threads because it uses synchronization methods. The state will not be damaged.

However, the usefulness of a Java vector is limited to multiple threads without additional synchronization. For example, consider the simple act of reading an element from a vector

 Vector vector = getVector(); if ( vector.size() > 0 ) { object first = vector.get(0); } 

This method will not spoil the state of the vector, but it is also incorrect. Nothing stops another thread from mutating a vector between an if statement and a get () call. This code may and will ultimately fail due to race conditions.

This type of synchronization is only useful in a few scenarios, and it is certainly not cheap. You pay a noticeable price for synchronization, even if you do not use multiple threads.

.Net decided not to pay this default price for a limited bounded scenario. Instead, he decided to implement a block-free list. The authors are responsible for adding any synchronization. It's closer to the C ++ model "pay only for what you use"

I recently wrote several articles about the dangers of using collections with internal synchronization, such as the Java vector.

Link to vector thread safety: http://www.ibm.com/developerworks/java/library/j-jtp09263.html

+67
Apr 01 '09 at 3:55
source share

Why would it be thread safe? Not every class. In fact, by default, classes are not thread safe.

Being thread safe will mean that any operation that modifies the list should be blocked from simultaneous access. This would be necessary even for lists that will be used by only one thread. That would be very inefficient.

+20
Apr 01 '09 at 3:54
source share

This is just a design solution for implementing stream-independent types. Collections provide the SyncRoot property of the SyncRoot interface and the Synchronized() method for some collections to explicitly synchronize data types.

Use SyncRoot to lock an object in multi-threaded environments.

 lock (collection.SyncRoot) { DoSomething(collection); } 

Use collection.Synchronized() to get a thread-safe wrapper for the collection.

+9
Apr 01 '09 at 3:58
source share

To ensure proper thread safety, List<> and other collection types must be immutable. With the parallel .NET extensions coming out in .NET 4.0, we will see thread-safe versions of the most commonly used collections. Jon Skeet touches on some of these.

+2
Apr 01 '09 at 5:58
source share

The likelihood of using the JaredPar race is a terrible consequence of its reliance on vector thread safety. This is what leads to the fact that "every ninth Tuesday the application does something strange" - an excerpt from the bug report that will drive you crazy.

There are a number of truly thread-safe collections included in .Net 4 , with an interesting side effect, threaded modification of the collection when enumerated , but there the performance that comes with streaming security is sometimes quite large.

Thus, the logical task for the infrastructure developer is to maintain the highest possible class for 95% of users who are likely to not process threads and rely on those who do multithreading to know that they have it safe.

+2
Apr 03 '09 at 18:18
source share

use SynchronizedCollection, it also provides a constructor parameter to use general synchronization :)

0
Jan 29 '10 at 12:39 on
source share



All Articles