Using immutable collections for thread-safe read-only operations in .NET

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:

Immutable package in NuGet

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.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: