Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Queue.Synchronized() Method in C#
The Queue.Synchronized() method in C# returns a thread-safe wrapper around an existing Queue object. This method is part of the System.Collections namespace and provides synchronization for multi-threaded applications where multiple threads need to access the same queue safely.
Syntax
Following is the syntax for the Queue.Synchronized() method −
public static System.Collections.Queue Synchronized(System.Collections.Queue queue);
Parameters
The method accepts one parameter −
queue − The Queue object to be wrapped with synchronization.
Return Value
Returns a synchronized (thread-safe) wrapper for the specified Queue. The wrapper provides synchronized access to all Queue operations.
How It Works
The synchronized wrapper ensures that all operations on the queue are thread-safe by internally using locks. You can check if a queue is synchronized using the IsSynchronized property, which returns true for synchronized queues and false for regular queues.
Using Queue.Synchronized() Method
Example 1: Basic Synchronization
using System;
using System.Collections;
public class Demo {
public static void Main() {
Queue queue = new Queue();
queue.Enqueue("AB");
queue.Enqueue("BC");
queue.Enqueue("CD");
queue.Enqueue("DE");
queue.Enqueue("EF");
queue.Enqueue("FG");
queue.Enqueue("GH");
queue.Enqueue("HI");
Console.WriteLine("Queue...");
IEnumerator demoEnum = queue.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine(demoEnum.Current);
}
Console.WriteLine("Is Queue synchronized? = " + queue.IsSynchronized);
Queue queue2 = Queue.Synchronized(queue);
Console.WriteLine("Is Queue synchronized now? = " + queue2.IsSynchronized);
}
}
The output of the above code is −
Queue... AB BC CD DE EF FG GH HI Is Queue synchronized? = False Is Queue synchronized now? = True
Example 2: Working with Synchronized Queue Operations
using System;
using System.Collections;
public class Demo {
public static void Main() {
Queue queue = new Queue();
queue.Enqueue(100);
queue.Enqueue(200);
queue.Enqueue(300);
Console.WriteLine("Is Queue synchronized? = " + queue.IsSynchronized);
Queue queue2 = Queue.Synchronized(queue);
Console.WriteLine("Is Queue synchronized now? = " + queue2.IsSynchronized);
Console.Write("Count of elements = ");
Console.WriteLine(queue.Count);
Console.WriteLine("Queue...");
foreach (int i in queue) {
Console.WriteLine(i);
}
Console.WriteLine("Does the queue has element 500? = " + queue.Contains(500));
int[] intArr = new int[5];
intArr[0] = 1;
intArr[1] = 2;
intArr[2] = 3;
intArr[3] = 4;
queue.CopyTo(intArr, 1);
Console.WriteLine("\nQueue (Updated)");
foreach (int i in queue) {
Console.WriteLine(i);
}
Console.WriteLine("\nArray (Updated)");
foreach (int i in intArr) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
Is Queue synchronized? = False Is Queue synchronized now? = True Count of elements = 3 Queue... 100 200 300 Does the queue has element 500? = False Queue (Updated) 100 200 300 Array (Updated) 1 100 200 300 0
Important Notes
The synchronized wrapper provides thread-safe access to all Queue operations including
Enqueue(),Dequeue(), and enumeration.When multiple threads access the same queue, use the synchronized version to prevent data corruption.
The original queue and synchronized wrapper share the same underlying data − changes to one affect the other.
For modern applications, consider using
ConcurrentQueue<T>fromSystem.Collections.Concurrentnamespace instead.
Conclusion
The Queue.Synchronized() method creates a thread-safe wrapper around an existing Queue, enabling safe concurrent access from multiple threads. While useful for legacy code, modern applications should prefer the generic ConcurrentQueue<T> for better performance and type safety.
