Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic Shared Function Synchronized ( _ list As ArrayList _ ) As ArrayList
Dim list As ArrayList Dim returnValue As ArrayList returnValue = ArrayList.Synchronized(list)
public static ArrayList Synchronized ( ArrayList list )
public: static ArrayList^ Synchronized ( ArrayList^ list )
public static ArrayList Synchronized ( ArrayList list )
public static function Synchronized ( list : ArrayList ) : ArrayList
Paramètres
- list
ArrayList à synchroniser.
Valeur de retour
Wrapper ArrayList synchronisé (thread-safe).
Notes Remarque |
|---|
| L'attribut HostProtectionAttribute appliqué à cette méthode a la valeur de propriété Resources suivante : Synchronization. HostProtectionAttribute n'affecte pas les applications bureautiques, qui sont généralement démarrées en double-cliquant sur une icône, en tapant une commande ou en entrant une URL dans un navigateur. Pour plus d'informations, consultez la classe HostProtectionAttribute ou Attributs de programmation et de protection des hôtes SQL Server. |
Afin de garantir la sécurité des threads de ArrayList, toutes les opérations doivent être effectuées par l'intermédiaire de ce wrapper.
L'énumération d'une collection n'est intrinsèquement pas une procédure thread-safe. Même lorsqu'une collection est synchronisée, les autres threads peuvent toujours modifier la collection, ce qui entraîne la levée d'une exception par l'énumérateur. Pour garantir la sécurité des threads au cours de l'énumération, vous pouvez soit verrouiller la collection pendant toute l'énumération, soit intercepter les exceptions résultant des modifications apportées par les autres threads.
ExempleL'exemple de code suivant montre comment verrouiller la collection à l'aide de SyncRoot au cours de l'énumération entière.
ArrayList myCollection = new ArrayList(); lock(myCollection.SyncRoot) { foreach (Object item in myCollection) { // Insert your code here. } }
Dim myCollection As New ArrayList() Dim item As Object SyncLock myCollection.SyncRoot For Each item In myCollection ' Insert your code here. Next item End SyncLock
Cette méthode est une opération O(1).
L'exemple de code suivant montre comment synchroniser ArrayList, déterminer si ArrayList est synchronisé et utiliser un ArrayList synchronisé.
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.
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. */
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. */
import System.*; import System.Collections.*; public class SamplesArrayList { public static void main(String[] args) { // 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.get_IsSynchronized() ? "synchronized" : "not synchronized"); Console.WriteLine("mySyncdAL is {0}.", mySyncdAL.get_IsSynchronized() ? "synchronized" : "not synchronized"); } //main } //SamplesArrayList /* This code produces the following output. myAL is not synchronized. mySyncdAL is synchronized. */
import System; import System.Collections; // Creates and initializes a new ArrayList. var myAL : ArrayList = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); // Creates a synchronized wrapper around the ArrayList. var mySyncdAL : ArrayList = 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. */
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Remarque
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ