ArrayList.Synchronized Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Restituisce un wrapper elenco sincronizzato (thread-safe).
Overload
| Nome | Descrizione |
|---|---|
| Synchronized(ArrayList) |
Restituisce un wrapper ArrayList sincronizzato (thread-safe). |
| Synchronized(IList) |
Restituisce un wrapper IList sincronizzato (thread-safe). |
Synchronized(ArrayList)
Restituisce un wrapper ArrayList sincronizzato (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
Parametri
Valori restituiti
Wrapper ArrayList sincronizzato (thread-safe).
Eccezioni
list è null.
Esempio
Nell'esempio di codice seguente viene illustrato come bloccare la raccolta usando SyncRoot durante l'intera enumerazione .
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
Questo metodo è un'operazione O(1) .
Nell'esempio di codice seguente viene illustrato come sincronizzare un oggetto ArrayList, determinare se un oggetto ArrayList è sincronizzato e usare un oggetto sincronizzato 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.
Commenti
Per garantire la thread safety di ArrayList, tutte le operazioni devono essere eseguite tramite questo wrapper.
L'enumerazione tramite una raccolta non è intrinsecamente una procedura thread-safe. Anche quando una raccolta viene sincronizzata, altri thread possono comunque modificare la raccolta, causando la generazione di un'eccezione da parte dell'enumeratore. Per garantire la thread safety durante l'enumerazione, è possibile bloccare la raccolta durante l'intera enumerazione o intercettare le eccezioni risultanti dalle modifiche apportate da altri thread.
Vedi anche
Si applica a
Synchronized(IList)
Restituisce un wrapper IList sincronizzato (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
Parametri
Valori restituiti
Wrapper IList sincronizzato (thread-safe).
Eccezioni
list è null.
Esempio
Nell'esempio di codice seguente viene illustrato come bloccare la raccolta usando SyncRoot durante l'intera enumerazione .
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
Questo metodo è un'operazione O(1) .
Commenti
Per garantire la thread safety di ArrayList, tutte le operazioni devono essere eseguite tramite questo wrapper.
L'enumerazione tramite una raccolta non è intrinsecamente una procedura thread-safe. Anche quando una raccolta viene sincronizzata, altri thread possono comunque modificare la raccolta, causando la generazione di un'eccezione da parte dell'enumeratore. Per garantire la thread safety durante l'enumerazione, è possibile bloccare la raccolta durante l'intera enumerazione o intercettare le eccezioni risultanti dalle modifiche apportate da altri thread.