Javatpoint Logo
Javatpoint Logo

Queue.Synchronized() in C#

The Queue.Synchronized() method in C# is a part of the System.Collections namespace. This method creates a thread-safe wrapper around a regular Queue instance. This method is more useful in a multi-threaded environment where multiple threads may access and modify the same Queue concurrently.

Syntax:

It has the following syntax:

The Queue object that you want to make thread-safe by creating a synchronized wrapper around it. It returns a thread-safe wrapper around the specified 'Queue'. This Queue does not need explicit locking in a multi-threaded environment.

There is a high risk of data corruption or unexcepted behaviour. To tackle this issue, synchronization mechanisms ensure that only one thread can access the data simultaneously. This method will create a synchronized wrapper around the given queue instance or object. The wrapper ensures that each operation on the Queue is atomic and thread-safe, meaning that no other thread can access the Queue during an ongoing operation. Synchronized Queue can be used in a multi-threaded context without additional locks or synchronization mechanisms.

Operations such as "Enqueue" and "Dequeue" are done in a thread-safe manner. The synchronization is applied at the level of individual method calls. So, each operation is protected against interference from other threads. It's important to note that it doesn't provide atomicity for compound operations. If you need to perform multiple operations as a single atomic operation, you may need to use additional synchronization mechanisms or consider using collections from the System.Collections.Concurrent namespace, which is designed for concurrent access. This method is part of the traditional collection classes in C#, and modern C# code often uses concurrent collections for improved performance in multi-threaded scenarios.

Example:

Let us take a program to illustrate the Queue.Synchronized() method in C#.

Output:

Queue.Synchronized() in C#

Explanation:

The program starts by importing the necessary libraries. A Queue named myQueue declared and initialize a new instance of the Queue class. It is a non-thread-safe Queue. A synchronized wrapper around the original myQueue is created using the Queue.Synchronized() method. It makes the myQueue a thread-safe-queue. It means multiple threads can safely access it without causing data corruption or unexpected behaviour. Three items are enqueued into the synchronized Queue using the Enqueue operation. After that, a while loop removes the items from the Queue. The while loop continues as long as items are in the synchronized Queue. The Count property returns the number of elements in the Queue. After that, the Dequeue operation removes and returns the object at the beginning of the Queue. It prints the dequeued item to the console. These queue operations are thread-safe. Without synchronization, simultaneous access by multiple threads could lead to unpredictable and erroneous behaviour. The synchronized wrapper helps prevent data corruption and ensures proper coordination among threads when accessing the shared Queue.

Example:

Let us take a C# program to demonstrate the Queue.Synchronized() method.

Output:

Queue.Synchronized() in C#

Explanation:

The program models a ticket counter where a regular queue represents the ticket queue. Use the Queue operation to ensure thread safety in a multi-threaded environment. The synchronized method is employed, creating a synchronized queue. Concurrent ticket purchases are simulated using tasks, each representing a person buying a ticket. The synchronized Queue ensures coordinated access, preventing data corruption during simultaneous operations. The tasks enqueue individuals into the Queue, reflecting successful ticket purchases. Simulated processing times between actions illustrate the program's thread-safe behaviour. Upon task completion, the program outputs a message indicating that all tickets have been sold and the synchronized queue is empty.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news