Is ArrayList/List a thread safe collection? If not how would you make it thread safe?
6 Answers
ArrayList/List is not a thread safe collection. We can make ArrayList a thread safe like:
using System;
using System.Collections;
public class SamplesArrayList
{
public static void Main()
{
// Creates and initializes a new ArrayList. It is thread safe ArrayList
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
// Creates a synchronized wrapper around the ArrayList.
ArrayList mySyncdAL = ArrayList.Synchronized(myAL);
// Displays the sychronization status of both ArrayLists.
Console.WriteLine( "myAL is {0}.", myAL.IsSynchronized ? "synchronized" : "not synchronized" );
Console.WriteLine( "mySyncdAL is {0}.", mySyncdAL.IsSynchronized ? "synchronized" : "not synchronized" );
}
}
This code produces the following output.
myAL is not synchronized. mySyncdAL is synchronized.
-
You are sure you want to use a .Net 1.0 API ArrayList, I thought no-one use this old DS any more. Concurrent namespace has much better options. – Mrinal Kamboj Sep 22 '14 at 14:44
-
Right you need to use latest version of .NET Framework. because everyone is updated with latest version .NET Framework. Or at least users have updated with .Net Framework 3.5/4/4.5.1. – Ash Sep 23 '14 at 13:45
-
@MrinalKamboj - what are the better options? This is one of the only readable examples of using a thread safe collection I've been able to find. – Mordy Jun 26 '18 at 08:47
No, according to msdn:
Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
You should use concurrent collections (msdn).
BlockingCollection Provides bounding and blocking functionality for any type that implements IProducerConsumerCollection.
ConcurrentDictionary Thread-safe implementation of a dictionary of key-value pairs
ConcurrentQueue Thread-safe implementation of a FIFO (first-in, first-out) queue.
ConcurrentStack Thread-safe implementation of a LIFO (last-in, first-out) stack.
ConcurrentBag Thread-safe implementation of an unordered collection of elements.
IProducerConsumerCollection The interface that a type must implement to be used in a BlockingCollection.

- 233,099
- 56
- 391
- 304
-
Can't get it, you mean static types are thread safe, in my view they are not for sure or I am misreading your post – Mrinal Kamboj Sep 22 '14 at 08:05
-
@MrinalKamboj it means that all static method / members of class are thread safe. – Sep 22 '14 at 08:14
-
I doubt, you mean a static List
in a class is thread safe ? static methods have nothing to do with the thread safety it is the static members which would matter. Try modifying static List – Mrinal Kamboj Sep 22 '14 at 14:33from multiple threads -
@MrinalKamboj not a static instance. I mean static method of class: http://www.completecsharptutorial.com/basic/staticmethod-variables.php. `Add` method is not static, so it is not thread safe. – Sep 22 '14 at 17:05
No, neither of them is.
The simplest way to make it thread-safe is to lock
all the access to the underlying collections (read and write). Of course, depending on your actual problem, it might simply be better to use a different collection, one that is thread-safe.

- 62,244
- 7
- 97
- 116
Check out my implementation using reader Writer lock, in case you really need one, but you would be better off using the Cocurrent APIs, they are guaranteed by MS

- 1
- 1

- 11,300
- 5
- 40
- 74
-
As in the comments of my implementation you would need an extra check in the implementations that fetch via index to ensure that index actually exist or they would be Null – Mrinal Kamboj Sep 22 '14 at 08:10
You can use SyncRoot property of the collection to make it thread safe msdn
ICollection myCollection = someCollection;
lock(myCollection.SyncRoot)
{
foreach (object item in myCollection)
{
// Insert your code here.
}
}

- 3,507
- 8
- 35
- 44