Home
C#
ConcurrentQueue and ConcurrentStack
Updated May 16, 2023
Dot Net Perls
ConcurrentQueue, ConcurrentStack. Queues and stacks are useful for many algorithms. We can handle elements in the order we have added them. But threads complicate matters.
Queue
Stack
With concurrent collections, located in System.Collections.Concurrent, we can safely use stacks and queues on threads. No ordering problems occur. We use methods like TryPeek.
ConcurrentQueue example. This is a poor example of ConcurrentQueue in a sense—it does not use multiple threads. It just shows the syntax of the queue.
Tip You can use the same syntax on multiple threads to use ConcurrentQueue. All methods (even ToArray) are safe.
using System; using System.Collections.Concurrent; class Program { static void Main() { // Create ConcurrentQueue. ConcurrentQueue<int> queue = new ConcurrentQueue<int>(); queue.Enqueue(10); queue.Enqueue(20); // Display queue contents. Console.WriteLine(string.Join(",", queue.ToArray())); // Get first element. if (queue.TryPeek(out int resultPeek)) { Console.WriteLine("TryPeek result:" + resultPeek); } // Get and remove first element. if (queue.TryDequeue(out int resultDequeue)) { Console.WriteLine("TryDeque result:" + resultDequeue); } // Display queue again. Console.WriteLine(string.Join(",", queue.ToArray())); } }
10,20 TryPeek result:10 TryDeque result:10 20
ConcurrentStack example. The ConcurrentStack has similar syntax to ConcurrentQueue. We use TryPeek in the same way. We use TryPop to remove an element (pop an element) from the stack.
using System; using System.Collections.Concurrent; class Program { static void Main() { // Create ConcurrentStack from array of elements. int[] elements = { 50, 10, 0 }; ConcurrentStack<int> stack = new ConcurrentStack<int>(elements); Console.WriteLine(string.Join(",", stack.ToArray())); // Push a new value to the stack. stack.Push(2000); Console.WriteLine(string.Join(",", stack.ToArray())); // Use TryPeek to get top of the stack. if (stack.TryPeek(out int resultPeek)) { Console.WriteLine("TryPeek result:" + resultPeek); } // Use TryPop to get and remove top of the stack. if (stack.TryPop(out int resultPop)) { Console.WriteLine("TryPop result:" + resultPop); } Console.WriteLine(string.Join(",", stack.ToArray())); } }
0,10,50 2000,0,10,50 TryPeek result:2000 TryPop result:2000 0,10,50
A summary. Concurrent collections are useful when you need them. In many programs, they are not needed—these they just impose a performance penalty.
ConcurrentDictionary
ConcurrentBag
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on May 16, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen