Stack.IsSynchronized Property in C#
Last Updated :
04 Feb, 2019
This method(comes under System.Collections namespace) is used to get a value indicating whether access to the Stack is synchronized (thread safe) or not. To guarantee the thread safety of the Stack, all operations must be done through the wrapper returned by the Synchronized method. Also, retrieving the value of this property is an O(1) operation.
Syntax:
public virtual bool IsSynchronized { get; }
Return Value: This property returns true, if access to the Stack is synchronized (thread safe) otherwise it returns false. The default is false.
Below programs illustrate the use of above-discussed property:
Example 1:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Stack myStack = new Stack();
myStack.Push( "Geeks" );
myStack.Push( "Geeks Classes" );
myStack.Push( "Noida" );
myStack.Push( "Data Structures" );
myStack.Push( "GeeksforGeeks" );
Stack ss = Stack.Synchronized(myStack);
Console.WriteLine( "myStack is {0}." , myStack.IsSynchronized ?
"Synchronized" : "Not Synchronized" );
Console.WriteLine( "ss is {0}." , ss.IsSynchronized ?
"Synchronized" : "Not Synchronized" );
}
}
|
Output:
myStack is Not Synchronized.
ss is Synchronized.
Example 2:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Stack myStack = new Stack();
myStack.Push( "1st" );
myStack.Push( "2nd" );
myStack.Push( "3rd" );
myStack.Push( "4th" );
myStack.Push( "5th" );
Console.WriteLine(myStack.IsSynchronized);
}
}
|
Reference:
Share your thoughts in the comments
Please Login to comment...