SortedList.Synchronized(SortedList) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
SortedList 개체에 대해 동기화되어 스레드로부터 안전하게 보호되는 래퍼를 반환합니다.
public:
static System::Collections::SortedList ^ Synchronized(System::Collections::SortedList ^ list);
public static System.Collections.SortedList Synchronized (System.Collections.SortedList list);
static member Synchronized : System.Collections.SortedList -> System.Collections.SortedList
Public Shared Function Synchronized (list As SortedList) As SortedList
매개 변수
- list
- SortedList
동기화할 SortedList 개체입니다.
반환
SortedList 개체에 대해 동기화되어 스레드로부터 안전하게 보호되는 래퍼입니다.
예외
list
이(가) null
인 경우
예제
다음 코드 예제에서는 전체 열거 하는 동안 속성을 사용 하 여 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 스레드 안전을 보장하려면 이 래퍼를 통해서만 모든 작업을 수행해야 합니다.
컬렉션 전체를 열거하는 프로시저는 기본적으로 스레드로부터 안전하지 않습니다. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.