SortedList.IsSynchronized プロパティ

定義

SortedList オブジェクトへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

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

プロパティ値

Boolean

true オブジェクトへのアクセスが同期されている (スレッド セーフである) 場合は SortedList。それ以外の場合は false。 既定値は、false です。

実装

次のコード例は、列挙体全体でプロパティを使用してコレクションを SyncRoot ロックする方法を示しています。

SortedList^ myCollection = gcnew SortedList();
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);
    }
}
SortedList myCollection = new SortedList();
lock (myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
Dim myCollection As New SortedList()
SyncLock myCollection.SyncRoot
    For Each item In myCollection
        ' Insert your code here.
    Next item
End SyncLock

このプロパティの値の取得は操作です O(1)

次のコード例は、オブジェクトを同期 SortedList する方法、同期されているかどうかを SortedList 判断する方法、および同期 SortedList済みオブジェクトを使用する方法を示しています。

#using <system.dll>

using namespace System;
using namespace System::Collections;
int main()
{
   
   // Creates and initializes a new SortedList.
   SortedList^ mySL = gcnew SortedList;
   mySL->Add( 2, "two" );
   mySL->Add( 3, "three" );
   mySL->Add( 1, "one" );
   mySL->Add( (int^)0, "zero" );
   mySL->Add( 4, "four" );
   
   // Creates a synchronized wrapper around the SortedList.
   SortedList^ mySyncdSL = SortedList::Synchronized( mySL );
   
   // Displays the sychronization status of both SortedLists.
   Console::WriteLine( "mySL is {0}.", mySL->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
   Console::WriteLine( "mySyncdSL is {0}.", mySyncdSL->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
}

/*
This code produces the following output.

mySL is not synchronized.
mySyncdSL is synchronized.
*/
using System;
using System.Collections;

public class SamplesSortedList
{
    public static void Main()
    {
        // Creates and initializes a new SortedList.
        SortedList mySL = new SortedList();
        mySL.Add(2, "two");
        mySL.Add(3, "three");
        mySL.Add(1, "one");
        mySL.Add(0, "zero");
        mySL.Add(4, "four");

        // Creates a synchronized wrapper around the SortedList.
        SortedList mySyncdSL = SortedList.Synchronized(mySL);

        // Displays the sychronization status of both SortedLists.
        Console.WriteLine("mySL is {0}.", mySL.IsSynchronized ? "synchronized" : "not synchronized");
        Console.WriteLine("mySyncdSL is {0}.", mySyncdSL.IsSynchronized ? "synchronized" : "not synchronized");
    }
}
/*
This code produces the following output.

mySL is not synchronized.
mySyncdSL is synchronized.
*/
Imports System.Collections

Public Class SamplesSortedList
    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new SortedList.
        Dim mySL As New SortedList()
        mySL.Add(2, "two")
        mySL.Add(3, "three")
        mySL.Add(1, "one")
        mySL.Add(0, "zero")
        mySL.Add(4, "four")
        
        ' Creates a synchronized wrapper around the SortedList.
        Dim mySyncdSL As SortedList = SortedList.Synchronized(mySL)
        
        ' Displays the sychronization status of both SortedLists.
        Dim msg As String
        If mySL.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("mySL is {0}.", msg)
        If mySyncdSL.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("mySyncdSL is {0}.", msg)        
    End Sub
End Class

' This code produces the following output.
'
' mySL is not synchronized.
' mySyncdSL is synchronized.

注釈

オブジェクトのスレッド セーフを SortedList 保証するには、メソッドによって返されるラッパーを通じてすべての操作を実行する Synchronized 必要があります。

コレクションの列挙処理は、本質的にスレッドセーフな処理ではありません。 コレクションが同期されていても、他のスレッドがコレクションを変更する場合があり、このときは列挙子から例外がスローされます。 列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

適用対象

こちらもご覧ください