ArrayList.Synchronized Método

Definição

Retorna um wrapper de lista sincronizado (thread safe).

Sobrecargas

Nome Description
Synchronized(ArrayList)

Retorna um ArrayList wrapper sincronizado (thread safe).

Synchronized(IList)

Retorna um IList wrapper sincronizado (thread safe).

Synchronized(ArrayList)

Retorna um ArrayList wrapper sincronizado (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

O ArrayList para sincronizar.

Retornos

Um ArrayList wrapper sincronizado (thread safe).

Exceções

list é null.

Exemplos

O exemplo de código a seguir mostra como bloquear a coleção usando a SyncRoot enumeração durante toda a enumeração.

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

Esse método é uma O(1) operação.

O exemplo de código a seguir mostra como sincronizar um ArrayList, determinar se um ArrayList é sincronizado e usar um sincronizado ArrayList.

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.

Comentários

Para garantir a segurança do thread, ArrayListtodas 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.

Confira também

Aplica-se a

Synchronized(IList)

Retorna um IList wrapper sincronizado (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

O IList para sincronizar.

Retornos

Um IList wrapper sincronizado (thread safe).

Exceções

list é null.

Exemplos

O exemplo de código a seguir mostra como bloquear a coleção usando a SyncRoot enumeração durante toda a enumeração.

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

Esse método é uma O(1) operação.

Comentários

Para garantir a segurança do thread, ArrayListtodas 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.

Confira também

Aplica-se a