Stack.IsSynchronized Propiedad

Definición

Obtiene un valor que indica si el acceso a la interfaz Stack está sincronizado (es seguro para subprocesos).

public:
 virtual property bool IsSynchronized { bool get(); };
public virtual bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public Overridable ReadOnly Property IsSynchronized As Boolean

Valor de propiedad

Es true si el acceso a Stack está sincronizado (es seguro para la ejecución de subprocesos); en caso contrario, false. De manera predeterminada, es false.

Implementaciones

Ejemplos

En el ejemplo siguiente se muestra cómo sincronizar un Stackobjeto , determinar si Stack se sincroniza y usar un objeto sincronizado 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.

Comentarios

Para garantizar la seguridad de subprocesos de Stack, todas las operaciones deben realizarse a través del contenedor devuelto por el Synchronized método .

Enumerar una colección no es intrínsecamente un procedimiento seguro para subprocesos. Incluso cuando una colección está sincronizada, otros subprocesos todavía pueden modificarla, lo que hace que el enumerador produzca una excepción. Con el fin de garantizar la seguridad para la ejecución de subprocesos durante la enumeración, se puede bloquear la colección durante toda la enumeración o detectar las excepciones resultantes de los cambios realizados por otros subprocesos.

En el ejemplo de código siguiente se muestra cómo bloquear la colección mediante durante SyncRoot toda la enumeración.

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

Recuperar el valor de esta propiedad es una O(1) operación.

Se aplica a