Queue.IsSynchronized Property in C#
This property is used get a value which indicates whether access to the Queue is synchronized (thread safe) or not.
Syntax:
public virtual bool IsSynchronized { get; }
Property Value: This property returns true if access to the Queue is synchronized(thread safe) otherwise, false. The default is false.
Below programs illustrate the use of the above-discussed property:
Example 1:
// C# code to illustrate the // Queue.IsSynchronized Property using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Queue Queue myQueue = new Queue(); // Inserting the elements into the Queue myQueue.Enqueue("C"); myQueue.Enqueue("C++"); myQueue.Enqueue("Java"); myQueue.Enqueue("C#"); myQueue.Enqueue("HTML"); myQueue.Enqueue("CSS"); // Creates a synchronized // wrapper around the Queue Queue sq = Queue.Synchronized(myQueue); // Displays the synchronization // status of both Queue Console.WriteLine("myQueue is {0}.", myQueue.IsSynchronized ? "Synchronized" : "Not Synchronized"); Console.WriteLine("sq is {0}.", sq.IsSynchronized ? "Synchronized" : "Not Synchronized"); } } |
chevron_right
filter_none
Output:
myQueue is Not Synchronized. sq is Synchronized.
Example 2:
// C# code to check if Queue // Is Synchronized or not using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Queue Queue myQueue = new Queue(); // Inserting the elements into the Queue myQueue.Enqueue(1); myQueue.Enqueue(2); myQueue.Enqueue(3); myQueue.Enqueue(4); // the default is false for // IsSynchronized property Console.WriteLine(myQueue.IsSynchronized); } } |
chevron_right
filter_none
Output:
False
Note:
- Retrieving the value of this property is an O(1) operation.
- To guarantee the thread safety of the Queue, all operations must be done through the wrapper returned by the Synchronized method.
- Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception.
Reference:
Recommended Posts:
- C# | SortedDictionary.Count Property
- C# | SortedDictionary.Values Property
- C# | SortedDictionary.Keys Property
- Queue.Count Property in C#
- Stack.IsSynchronized Property in C#
- Stack.Count Property in C#
- C# | Dictionary.Keys Property
- C# | Dictionary.Values Property
- C# | Dictionary.Item[] Property
- C# | Dictionary.Count Property
- C# | SortedDictionary.Item[] Property
- Thread.CurrentThread Property in C#
- How to use AutoCheck property of CheckBox in C#?
- How to use AcceptsReturn Property of TextBox in C#?
- Console.KeyAvailable() Property in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



