ArrayList.IsSynchronized-Eigenschaft

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

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable ReadOnly Property IsSynchronized As Boolean
'Usage
Dim instance As ArrayList
Dim value As Boolean

value = instance.IsSynchronized
public virtual bool IsSynchronized { get; }
public:
virtual property bool IsSynchronized {
    bool get ();
}
/** @property */
public boolean get_IsSynchronized ()
public function get IsSynchronized () : boolean

Eigenschaftenwert

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

Hinweise

Zur Gewährleistung der Threadsicherheit von ArrayList müssen alle Operationen unter Verwendung des Wrappers ausgeführt werden, der von der Synchronized-Methode zurückgegeben wird.

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

Beispiel

Das folgende Codebeispiel veranschaulicht, wie die Auflistung mithilfe von SyncRoot während der gesamten Enumeration gesperrt wird:

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

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

Im folgenden Codebeispiel wird veranschaulicht, wie Sie eine ArrayList synchronisieren und eine synchronisierte ArrayList verwenden und wie Sie ermitteln, ob eine ArrayList synchronisiert ist.

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.
 */ 

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

ArrayList-Klasse
ArrayList-Member
System.Collections-Namespace
SyncRoot
Synchronized