ArrayList.Synchronized ArrayList.Synchronized ArrayList.Synchronized ArrayList.Synchronized Method

Definição

Retorna um wrapper de lista sincronizado (thread-safe).Returns a list wrapper that is synchronized (thread safe).

Sobrecargas

Synchronized(ArrayList) Synchronized(ArrayList) Synchronized(ArrayList) Synchronized(ArrayList)

Retorna um wrapper ArrayList sincronizado (thread-safe).Returns an ArrayList wrapper that is synchronized (thread safe).

Synchronized(IList) Synchronized(IList) Synchronized(IList) Synchronized(IList)

Retorna um wrapper IList sincronizado (thread-safe).Returns an IList wrapper that is synchronized (thread safe).

Synchronized(ArrayList) Synchronized(ArrayList) Synchronized(ArrayList) Synchronized(ArrayList)

Retorna um wrapper ArrayList sincronizado (thread-safe).Returns an ArrayList wrapper that is synchronized (thread safe).

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 ArrayList ArrayList

O ArrayList a ser sincronizado.The ArrayList to synchronize.

Retornos

Um wrapper ArrayList sincronizado (thread-safe).An ArrayList wrapper that is synchronized (thread safe).

Exceções

Exemplos

O exemplo de código a seguir mostra como bloquear a coleção usando o SyncRoot durante toda a enumeração.The following code example shows how to lock the collection using the SyncRoot during the entire enumeration.

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 é uma operação O(1).This method is an O(1) operation.

O exemplo de código a seguir mostra como sincronizar ArrayListum, determinar se ArrayList um está sincronizado e usar ArrayListum sincronizado.The following code example shows how to synchronize an ArrayList, determine if an ArrayList is synchronized and use a synchronized 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
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. 

Comentários

Para garantir a segurança do thread do ArrayList, todas as operações devem ser feitas por meio desse wrapper.To guarantee the thread safety of the ArrayList, all operations must be done through this wrapper.

A enumeração por meio de uma coleção não é um procedimento thread-safe intrínseco.Enumerating through a collection is intrinsically not a thread-safe procedure. Mesmo quando uma coleção está sincronizada, outros threads ainda podem modificar a coleção, o que faz o enumerador lançar uma exceção.Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. Para garantir thread-safe durante a enumeração, é possível bloquear a coleção durante toda a enumeração ou verificar as exceções resultantes das alterações feitas por outros threads.To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Veja também

Synchronized(IList) Synchronized(IList) Synchronized(IList) Synchronized(IList)

Retorna um wrapper IList sincronizado (thread-safe).Returns an IList wrapper that is synchronized (thread safe).

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 IList IList

O IList a ser sincronizado.The IList to synchronize.

Retornos

Um wrapper IList sincronizado (thread-safe).An IList wrapper that is synchronized (thread safe).

Exceções

Exemplos

O exemplo de código a seguir mostra como bloquear a coleção usando o SyncRoot durante toda a enumeração.The following code example shows how to lock the collection using the SyncRoot during the entire enumeration.

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 é uma operação O(1).This method is an O(1) operation.

Comentários

Para garantir a segurança do thread do ArrayList, todas as operações devem ser feitas por meio desse wrapper.To guarantee the thread safety of the ArrayList, all operations must be done through this wrapper.

A enumeração por meio de uma coleção não é um procedimento thread-safe intrínseco.Enumerating through a collection is intrinsically not a thread-safe procedure. Mesmo quando uma coleção está sincronizada, outros threads ainda podem modificar a coleção, o que faz o enumerador lançar uma exceção.Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. Para garantir thread-safe durante a enumeração, é possível bloquear a coleção durante toda a enumeração ou verificar as exceções resultantes das alterações feitas por outros threads.To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Veja também

Aplica-se a