Queue.IsSynchronized Property in C#


The Queue.IsSynchronized() method in C# is used to GET a value indicating whether access to the Queue is synchronized (thread safe).

Syntax

The syntax is as follows −

public virtual bool IsSynchronized { get; }

Example

Let us now see an example −

 Live Demo

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);
      queue.Enqueue(400);
      queue.Enqueue(500);
      queue.Enqueue(600);
      queue.Enqueue(700);
      queue.Enqueue(800);
      queue.Enqueue(900);
      queue.Enqueue(1000);
      Console.WriteLine("Queue...");
      IEnumerator demoEnum = queue.GetEnumerator();
      while (demoEnum.MoveNext()) {
         Console.WriteLine(demoEnum.Current);
      }
      Console.WriteLine("Is Queue synchronized? = "+queue.IsSynchronized);
   }
}

Output

This will produce the following output −

Queue...
100
200
300
400
500
600
700
800
900
1000
Is Queue synchronized? = False

Example

Let us now see another example −

 Live Demo

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);
   }
}

Output

This will produce the following output −

Queue...
AB
BC
CD
DE
EF
FG
GH
HI
Is Queue synchronized? = False
Is Queue synchronized now? = True
raja
Published on 04-Dec-2019 12:07:10
Advertisements