ArrayList.IsSynchronized Eigenschaft

Definition

Ruft einen Wert ab, der angibt, ob der Zugriff auf die ArrayList synchronisiert (threadsicher) ist.

public:
 virtual property bool IsSynchronized { bool get(); };
public virtual bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public Overridable ReadOnly Property IsSynchronized As Boolean

Eigenschaftswert

true, wenn der Zugriff auf das ArrayList synchronisiert (threadsicher) ist, andernfalls false. Der Standardwert ist false.

Implementiert

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie die Auflistung während der gesamten Enumeration mit gesperrt SyncRoot wird.

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

Das Abrufen des Werts dieser Eigenschaft ist ein O(1) Vorgang.

Im folgenden Codebeispiel wird gezeigt, wie ein ArrayListsynchronisiert wird, bestimmen, ob eine ArrayList synchronisiert wird, und verwenden Sie eine synchronisierte 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.

Hinweise

Um die Threadsicherheit von ArrayListzu gewährleisten, müssen alle Vorgänge über den von der Synchronized -Methode zurückgegebenen Wrapper erfolgen.

Die Enumeration einer Auflistung ist systemintern keine threadsichere Prozedur. Selbst wenn eine Auflistung synchronisiert wird, besteht die Möglichkeit, dass andere Threads sie ändern. Dies führt dazu, dass der Enumerator eine Ausnahme auslöst. Um während der Enumeration Threadsicherheit zu gewährleisten, können Sie entweder die Auflistung während der gesamten Enumeration sperren oder die Ausnahmen, die aus von anderen Threads stammenden Änderungen resultieren, abfangen.

Gilt für:

Weitere Informationen