Stack.Synchronized(Stack) Método
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Retorna um wrapper sincronizado (thread safe) para o 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
Parâmetros
Retornos
Um wrapper sincronizado ao redor do Stack.
Exceções
stack é null.
Exemplos
O exemplo a seguir mostra como sincronizar um Stack, determinar se um Stack é sincronizado e usar um sincronizado 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.
Comentários
Para garantir a segurança do thread, Stacktodas as operações devem ser feitas por meio desse wrapper.
Enumerar por meio de uma coleção não é intrinsecamente um procedimento thread-safe. Mesmo quando uma coleção é sincronizada, outros threads ainda podem modificar a coleção, o que faz com que o enumerador gere uma exceção. Para garantir a segurança do thread durante a enumeração, você pode bloquear a coleção durante toda a enumeração ou capturar as exceções resultantes de alterações feitas por outros threads.
O exemplo de código a seguir mostra como bloquear a coleção usando a SyncRoot enumeração durante toda a enumeração.
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
Esse método é uma O(1) operação.