Using immutable collections for thread-safe read-only operations in .NET
September 23, 2015 1 Comment
Sometimes you have a scenario where multiple threads need to read from the same shared collection. We’ve looked at the 4 concurrent, i.e. thread-safe collection types on this blog that are available in the System.Collections.Concurrent namespace. They can be safely used for both concurrent writes and reads.
However, if your threads strictly only need to read from a collection then there’s another option. There are collections in the System.Collections.Immutable namespace that are immutable, i.e. read-only and have been optimisied for concurrent read operations.
The Immutable namespace is not available in the standard .NET packages. You’ll have to load it from NuGet. Also, they are only supported in .NET4.5 or higher:
Consider the following Guest class:
public class Guest { public string Name { get; set; } public int Age { get; set; } }
Nothing special there I believe.
The System.Collections.Immutable includes an immutable version of all popular collection types: ImmutableList, ImmutableArray, ImmutableDictionary, ImmutableHashSet etc. You can check what’s available through intellisense.
Let’s see an example of ImmutableList of T.
An immutable list can be instantiated through a static Create method, i.e. it has no public constructor. The Create method accepts one or more objects that the collection will contain.
The following sample demonstrates the object initialisation. It also shows how 5 threads are started to randomly select an item from the collection and print the Guest name. The threads sleep 1 second before extracting the next random element:
public class ImmutableCollectionSampleService { private ImmutableList<Guest> _immutableStudents; public ImmutableCollectionSampleService() { _immutableStudents = ImmutableList.Create(new Guest() { Age = 25, Name = "John" } , new Guest() { Age = 24, Name = "Barbara" }, new Guest() { Age = 24, Name = "Phil" } , new Guest() { Age = 23, Name = "Fred" }, new Guest() { Age = 26, Name = "Hannah" } , new Guest() { Age = 27, Name = "Cindy" }); } public void RunImmutableCollectionSample() { Task.Run(() => PrintRandomElementFromImmutableList()); Task.Run(() => PrintRandomElementFromImmutableList()); Task.Run(() => PrintRandomElementFromImmutableList()); Task.Run(() => PrintRandomElementFromImmutableList()); Task.Run(() => PrintRandomElementFromImmutableList()); } private void PrintRandomElementFromImmutableList() { Debug.WriteLine(string.Format("Thread {0} starting to read from immutable list.", Thread.CurrentThread.ManagedThreadId)); int collectionSize = _immutableStudents.Count; while (true) { Random random = new Random(); int index = random.Next(collectionSize); Debug.WriteLine(_immutableStudents[index].Name); Thread.Sleep(1000); } } }
Here’s a sample output in the debug window:
Thread 10 starting to read from immutable list.
Thread 13 starting to read from immutable list.
Thread 12 starting to read from immutable list.
Thread 11 starting to read from immutable list.
Hannah
Cindy
Hannah
Hannah
Thread 14 starting to read from immutable list.
Barbara
Hannah
Cindy
Hannah
View the list of posts on the Task Parallel Library here.
Thanks for the post! When using Immutable containers, I recommend making the objects they hold immutable as well:
public class Guest
{
public Guest(string name, int age)
{
Name = name;
Age = age;
}
public string Name { get; private set; }
public int Age { get; private set; }
}