Stack.Synchronized 方法
返回 Stack 的同步(线程安全)包装。
**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Function Synchronized ( _
stack As Stack _
) As Stack
用法
Dim stack As Stack
Dim returnValue As Stack
returnValue = Stack.Synchronized(stack)
public static Stack Synchronized (
Stack stack
)
public:
static Stack^ Synchronized (
Stack^ stack
)
public static Stack Synchronized (
Stack stack
)
public static function Synchronized (
stack : Stack
) : Stack
参数
- stack
要同步的 Stack。
返回值
Stack 周围的同步包装。
异常
异常类型 | 条件 |
---|---|
stack 为 空引用(在 Visual Basic 中为 Nothing)。 |
备注
提示
应用于此方法的 HostProtectionAttribute 属性 (Attribute) 具有以下 Resources 属性 (Property) 值:Synchronization。HostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标,键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性。
若要保证 Stack 的线程安全,则必须通过此包装执行所有操作。
通过集合枚举在本质上不是一个线程安全的过程。即使一个集合已进行同步,其他线程仍可以修改该集合,这将导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。
下面的代码示例演示如何在整个枚举过程中使用 SyncRoot 锁定集合。
Stack myCollection = new Stack();
lock(myCollection.SyncRoot) {
foreach (Object item in myCollection) {
// Insert your code here.
}
}
Dim myCollection As New Stack()
Dim item As Object
SyncLock myCollection.SyncRoot
For Each item In myCollection
' Insert your code here.
Next item
End SyncLock
此方法的运算复杂度为 O(1)。
示例
下面的示例演示如何同步 Stack、如何确定 Stack 是否同步以及如何使用同步的 Stack。
Imports System
Imports System.Collections
Public Class SamplesStack
Public Shared Sub Main()
' Creates and initializes a new Stack.
Dim myStack As New Stack()
myStack.Push("The")
myStack.Push("quick")
myStack.Push("brown")
myStack.Push("fox")
' Creates a synchronized wrapper around the Stack.
Dim mySyncdStack As Stack = Stack.Synchronized(myStack)
' Displays the sychronization status of both Stacks.
Dim msg As String
If myStack.IsSynchronized Then
msg = "synchronized"
Else
msg = "not synchronized"
End If
Console.WriteLine("myStack is {0}.", msg)
If mySyncdStack.IsSynchronized Then
msg = "synchronized"
Else
msg = "not synchronized"
End If
Console.WriteLine("mySyncdStack is {0}.", msg)
End Sub
End Class
' This code produces the following output.
'
' myStack is not synchronized.
' mySyncdStack is synchronized.
using System;
using System.Collections;
public class SamplesStack {
public static void Main() {
// Creates and initializes a new Stack.
Stack myStack = new Stack();
myStack.Push( "The" );
myStack.Push( "quick" );
myStack.Push( "brown" );
myStack.Push( "fox" );
// Creates a synchronized wrapper around the Stack.
Stack mySyncdStack = Stack.Synchronized( myStack );
// Displays the sychronization status of both Stacks.
Console.WriteLine( "myStack is {0}.",
myStack.IsSynchronized ? "synchronized" : "not synchronized" );
Console.WriteLine( "mySyncdStack is {0}.",
mySyncdStack.IsSynchronized ? "synchronized" : "not synchronized" );
}
}
/*
This code produces the following output.
myStack is not synchronized.
mySyncdStack is synchronized.
*/
#using <system.dll>
using namespace System;
using namespace System::Collections;
int main()
{
// Creates and initializes a new Stack.
Stack^ myStack = gcnew Stack;
myStack->Push( "The" );
myStack->Push( "quick" );
myStack->Push( "brown" );
myStack->Push( "fox" );
// Creates a synchronized wrapper around the Stack.
Stack^ mySyncdStack = Stack::Synchronized( myStack );
// Displays the sychronization status of both Stacks.
Console::WriteLine( "myStack is {0}.", myStack->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
Console::WriteLine( "mySyncdStack is {0}.", mySyncdStack->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
}
/*
This code produces the following output.
myStack is not synchronized.
mySyncdStack is synchronized.
*/
import System.*;
import System.Collections.*;
public class SamplesStack
{
public static void main(String[] args)
{
// Creates and initializes a new Stack.
Stack myStack = new Stack();
myStack.Push("The");
myStack.Push("quick");
myStack.Push("brown");
myStack.Push("fox");
// Creates a synchronized wrapper around the Stack.
Stack mySyncdStack = Stack.Synchronized(myStack);
// Displays the sychronization status of both Stacks.
Console.WriteLine("myStack is {0}.",
(myStack.get_IsSynchronized()) ?
"synchronized" : "not synchronized");
Console.WriteLine("mySyncdStack is {0}.",
(mySyncdStack.get_IsSynchronized()) ?
"synchronized" : "not synchronized");
} //main
} //SamplesStack
/*
This code produces the following output.
myStack is not synchronized.
mySyncdStack is synchronized.
*/
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0