Stack.Synchronized(Stack) Metoda

Definice

Vrátí synchronizovanou obálku (bezpečný pro vlákno Stack) pro .

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

Parametry

stack
Stack

Synchronizace:Stack

Návraty

Synchronizovaný obálka Stackkolem .

Výjimky

stack je null.

Příklady

Následující příklad ukazuje, jak synchronizovat Stack, určit, zda Stack je synchronizován, a použít synchronizovaný Stack.

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.

Poznámky

Chcete-li zaručit bezpečnost Stackvláken , musí být všechny operace provedeny prostřednictvím tohoto obálky.

Výčet prostřednictvím kolekce není vnitřně bezpečným postupem pro přístup z více vláken. I když je kolekce synchronizována, ostatní vlákna mohou stále upravovat kolekci, což způsobí, že enumerátor vyvolá výjimku. Chcete-li zaručit bezpečnost vláken během výčtu, můžete buď uzamknout kolekci během celého výčtu, nebo zachytit výjimky vyplývající z změn provedených jinými vlákny.

Následující příklad kódu ukazuje, jak uzamknout kolekci pomocí celého výčtu 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

Tato metoda je O(1) operace.

Platí pro