此方法(位于System.Collections命名空间下)用于获取一个值,该值指示对堆栈的访问是否同步(线程安全)。为了保证Stack的线程安全,所有操作必须通过Synchronized方法返回的包装器完成。同样,检索此属性的值是O(1)操作。
句法:
public virtual bool IsSynchronized { get; }
返回值:如果同步访问堆栈(线程安全),则此属性返回true ,否则返回false 。默认值为false 。
下面的程序说明了上面讨论的属性的用法:
范例1:
// C# code to illustrate the
// Stack.IsSynchronized Property
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack
Stack myStack = new Stack();
// Inserting the elements into the Stack
myStack.Push("Geeks");
myStack.Push("Geeks Classes");
myStack.Push("Noida");
myStack.Push("Data Structures");
myStack.Push("GeeksforGeeks");
// Creates a synchronized
// wrapper around the Stack
Stack ss = Stack.Synchronized(myStack);
// Displaying the synchronization
// status of both Stack
Console.WriteLine("myStack is {0}.", myStack.IsSynchronized ?
"Synchronized" : "Not Synchronized");
Console.WriteLine("ss is {0}.", ss.IsSynchronized ?
"Synchronized" : "Not Synchronized");
}
}
输出:
myStack is Not Synchronized.
ss is Synchronized.
范例2:
// C# code to illustrate the
// Stack.IsSynchronized Property
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack
Stack myStack = new Stack();
// Inserting elements into Stack
myStack.Push("1st");
myStack.Push("2nd");
myStack.Push("3rd");
myStack.Push("4th");
myStack.Push("5th");
// the default is false for
// IsSynchronized property
Console.WriteLine(myStack.IsSynchronized);
}
}
输出:
False
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.stack.issynchronized?view=netframework-4.7.2