ArrayList.Synchronized Método

Definición

Devuelve un contenedor de lista que está sincronizado (es seguro para subprocesos).

Sobrecargas

Synchronized(ArrayList)

Devuelve un contenedor de la clase ArrayList que está sincronizado (es seguro para subprocesos).

Synchronized(IList)

Devuelve un contenedor de la clase IList que está sincronizado (es seguro para subprocesos).

Synchronized(ArrayList)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

Devuelve un contenedor de la clase ArrayList que está sincronizado (es seguro para subprocesos).

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

Parámetros

list
ArrayList

ArrayList que se va a sincronizar.

Devoluciones

Contenedor de la clase ArrayList que está sincronizado (es seguro para subprocesos).

Excepciones

list es null.

Ejemplos

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

ArrayList^ myCollection = gcnew ArrayList();
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);
    }
}
ArrayList myCollection = new ArrayList();

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

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

Este método es una O(1) operación.

En el ejemplo de código siguiente se muestra cómo sincronizar un , ArrayListdeterminar si se ArrayList sincroniza y se usa un objeto sincronizado ArrayList.

using namespace System;
using namespace System::Collections;
int main()
{
   
   // Creates and initializes a new ArrayList instance.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "The" );
   myAL->Add( "quick" );
   myAL->Add( "brown" );
   myAL->Add( "fox" );
   
   // Creates a synchronized wrapper around the ArrayList.
   ArrayList^ mySyncdAL = ArrayList::Synchronized( myAL );
   
   // Displays the sychronization status of both ArrayLists.
   String^ szRes = myAL->IsSynchronized ?  (String^)"synchronized" :  "not synchronized";
   Console::WriteLine(  "myAL is {0}.", szRes );
   String^ szSyncRes = mySyncdAL->IsSynchronized ?  (String^)"synchronized" :  "not synchronized";
   Console::WriteLine(  "mySyncdAL is {0}.", szSyncRes );
}

/* 
 This code produces the following output.
 
 myAL is not synchronized.
 mySyncdAL is synchronized.
 */
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add( "The" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );

      // Creates a synchronized wrapper around the ArrayList.
      ArrayList mySyncdAL = ArrayList.Synchronized( myAL );

      // Displays the sychronization status of both ArrayLists.
      Console.WriteLine( "myAL is {0}.", myAL.IsSynchronized ? "synchronized" : "not synchronized" );
      Console.WriteLine( "mySyncdAL is {0}.", mySyncdAL.IsSynchronized ? "synchronized" : "not synchronized" );
   }
}
/*
This code produces the following output.

myAL is not synchronized.
mySyncdAL is synchronized.
*/
Imports System.Collections

Public Class SamplesArrayList
    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new ArrayList.
        Dim myAL As New ArrayList()
        myAL.Add("The")
        myAL.Add("quick")
        myAL.Add("brown")
        myAL.Add("fox")
        
        ' Creates a synchronized wrapper around the ArrayList.
        Dim mySyncdAL As ArrayList = ArrayList.Synchronized(myAL)
        
        ' Displays the sychronization status of both ArrayLists.
        Dim str As String
        If myAL.IsSynchronized Then
            str = "synchronized"
        Else
            str = "not synchronized"
        End If
        Console.WriteLine("myAL is {0}.", str)
        If mySyncdAL.IsSynchronized Then
            str = "synchronized"
        Else
            str = "not synchronized"
        End If
        Console.WriteLine("mySyncdAL is {0}.", str)
    End Sub
End Class

' This code produces the following output.
' 
' myAL is not synchronized.
' mySyncdAL is synchronized.

Comentarios

Para garantizar la seguridad de subprocesos de ArrayList, todas las operaciones deben realizarse a través de este contenedor.

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.

Consulte también

Se aplica a

Synchronized(IList)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

Devuelve un contenedor de la clase IList que está sincronizado (es seguro para subprocesos).

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

Parámetros

list
IList

IList que se va a sincronizar.

Devoluciones

Contenedor de la clase IList que está sincronizado (es seguro para subprocesos).

Excepciones

list es null.

Ejemplos

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

ArrayList^ myCollection = gcnew ArrayList();
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);
    }
}
ArrayList myCollection = new ArrayList();

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

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

Este método es una O(1) operación.

Comentarios

Para garantizar la seguridad de subprocesos de ArrayList, todas las operaciones deben realizarse a través de este contenedor.

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.

Consulte también

Se aplica a