ArrayList.IsSynchronized プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)


ArrayList を確実にスレッド セーフにするためには、すべての操作を Synchronized メソッドから返されるラッパー経由で実行する必要があります。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

列挙処理中に SyncRoot を使用してコレクションをロックする方法を次のコード例に示します。
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
このプロパティ値を取得することは、O(1) 操作になります。
ArrayList を同期する方法と、ArrayList が同期されているかどうかを確認し、同期済みの ArrayList を使用する方法を次のコード例に示します。
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. */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からArrayList.IsSynchronized プロパティを検索する場合は、下記のリンクをクリックしてください。

- ArrayList.IsSynchronized プロパティのページへのリンク