ArrayList.IsSynchronized 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
ArrayList에 대한 액세스가 동기화되어 스레드로부터 안전하게 보호되는지를 나타내는 값을 가져옵니다.
public:
virtual property bool IsSynchronized { bool get(); };
public virtual bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public Overridable ReadOnly Property IsSynchronized As Boolean
속성 값
true
에 대한 액세스가 동기화되어 스레드로부터 안전하게 보호되면 ArrayList이고, 그렇지 않으면 false
입니다. 기본값은 false
입니다.
구현
예제
다음 코드 예제에서는 전체 열거형 동안 을 SyncRoot 사용하여 컬렉션을 잠그는 방법을 보여줍니다.
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
이 속성의 값을 검색하는 작업은 작업입니다 O(1)
.
다음 코드 예제에서는 동기화 ArrayList하는 방법을 보여 집니다 합니다 동기화 되는지 ArrayList 여부를 확인 하 고 동기화 된 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.
설명
의 ArrayList스레드 안전을 보장하려면 메서드에서 반환 Synchronized 된 래퍼를 통해 모든 작업을 수행해야 합니다.
컬렉션 전체를 열거하는 프로시저는 기본적으로 스레드로부터 안전하지 않습니다. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.