From the following site:
http://crfdesign.net/programming/top-10-differences-between-java-and-c
Unfortunately,
List<>
is not thread-safe (C#’sArrayList
and Java’sVector
are thread-safe). C# also has aHashtable
; the generic version is:
What makes List<T>
not thread-safe? Is it implementation problem on .NET framework engineer's part? Or are generics not thread-safe?
A thread-safe variant of ArrayList in which all mutative operations (e.g., add, set, remove..) are implemented by creating a separate copy of an underlying array. It achieves thread safety by creating a separate copy of the List which is different than vector or other collections used to provide thread-safety.
Vectors are synchronized. Any method that touches the Vector 's contents is thread safe. ArrayList , on the other hand, is unsynchronized, making them, therefore, not thread safe.
Not thread safe: Data structures should not be accessed simultaneously by different threads.
NET Framework 4 introduces the System. Collections. Concurrent namespace, which includes several collection classes that are both thread-safe and scalable. Multiple threads can safely and efficiently add or remove items from these collections, without requiring additional synchronization in user code.
You really need to classify Java's Vector's type of thread safety. Javas Vector is safe to be used from multiple threads because it uses synchronization on the methods. State will not be corrupted.
However, Java's vector's usefulness is limited from 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 corrupt the state of the vector, but it also is not correct. There is nothing stopping another thread from mutating the vector in between the if statement an the get() call. This code can and will eventually fail because of a race condition.
This type of synchronization is only useful in a handfull of scenarios and it is certainly not cheap. You pay a noticable price for synchronization even if you don't use multiple threads.
.Net chose not to pay this price by default for a scenario of only limited usefulness. Instead it chose to implement a lock free List. Authors are responsible for adding any synchronization. It's closer to C++'s model of "pay only for what you use"
I recently wrote a couple of articles on the dangers of using collections with only internal synchronization such as Java's vector.
Reference Vector thread safety: http://www.ibm.com/developerworks/java/library/j-jtp09263.html
Why would it be thread-safe? Not every class is. In fact, by default, classes are not thread-safe.
Being thread-safe would mean that any operation modifying the list would need to be interlocked against simultaneous access. This would be necessary even for those lists that will only ever be used by a single thread. That would be very inefficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With