Stack.Synchronized(Stack) 方法

定义

返回 Stack 的同步(线程安全)包装。

public:
 static System::Collections::Stack ^ Synchronized(System::Collections::Stack ^ stack);
public static System.Collections.Stack Synchronized (System.Collections.Stack stack);
static member Synchronized : System.Collections.Stack -> System.Collections.Stack
Public Shared Function Synchronized (stack As Stack) As Stack

参数

stack
Stack

要同步的 Stack

返回

Stack

Stack 周围的同步包装。

例外

stacknull

示例

以下示例演示如何同步、 Stack确定 Stack 是否已同步和使用同步 Stack

#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.
*/
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.
*/
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.

注解

若要保证线程安全性 Stack,必须通过此包装器执行所有操作。

枚举整个集合本质上不是一个线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。

下面的代码示例演示如何使用 SyncRoot 整个枚举期间锁定集合。

Stack^ myCollection = gcnew Stack();
bool lockTaken = false;
try
{
    Monitor::Enter(myCollection->SyncRoot, lockTaken);
    for each (Object^ item in myCollection);
    {
        // Insert your code here.
    }
}
finally
{
    if (lockTaken)
    {
        Monitor::Exit(myCollection->SyncRoot);
    }
}
Stack myCollection = new Stack();

lock (myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
Dim myCollection As New Stack()

SyncLock myCollection.SyncRoot
    For Each item As Object In myCollection
        ' Insert your code here.
    Next item
End SyncLock

此方法是一个 O(1) 操作。

适用于