C#에서 StringDictionary가 동기화(synchronized)되어 있는지 확인하려면 IsSynchronized 속성을 사용하면 됩니다. 이 속성은 해당 컬렉션에 대한 접근이 스레드로부터 안전(thread-safe)하게 동기화되어 있는지 여부를 bool 값으로 반환합니다.
예제 1
두 개의 StringDictionary를 생성하고 요소를 추가한 뒤, IsSynchronized 속성 값이 어떻게 출력되는지 확인해 보겠습니다.
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 elements...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("\nStringDictionary2 key-value pairs...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Is the StringDictionary2 synchronized? = " + strDict2.IsSynchronized);
}
}
출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다 −
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E Is the StringDictionary2 synchronized? = False
참고로 StringDictionary는 키(key)를 자동으로 소문자로 변환하여 저장하기 때문에 출력 결과에서 키가 소문자로 표시됩니다.
예제 2
이번에는 다른 예제를 통해 IsSynchronized 속성을 다시 한번 확인해 보겠습니다 −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("One", "Laptop");
strDict.Add("Two", "Desktop");
strDict.Add("Three", "Earphone");
strDict.Add("Four", "Speaker");
strDict.Add("Five", "HardDisk");
strDict.Add("Six", "SSD");
strDict.Add("Seven", "HardDisk");
Console.WriteLine("StringDictionary key-value pairs...");
IEnumerator demoEnum = strDict.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Is StringDictionary synchronized? = " + strDict.IsSynchronized);
}
}
출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다 −
StringDictionary key-value pairs... Key = five, Value = HardDisk Key = one, Value = Laptop Key = four, Value = Speaker Key = three, Value = Earphone Key = seven, Value = HardDisk Key = six, Value = SSD Key = two, Value = Desktop Is StringDictionary synchronized? = False
정리
IsSynchronized 속성은 StringDictionary에 대한 액세스가 동기화되어 스레드로부터 안전한지 여부를 나타냅니다. 일반적인 방식으로 생성한 StringDictionary는 동기화되어 있지 않으므로 위 예제들처럼 항상 False가 반환됩니다. 멀티스레드 환경에서 안전하게 사용하려면 Synchronized() 메서드로 감싼 동기화된 래퍼(wrapper) 객체를 사용해야 하며, 이 경우 IsSynchronized는 True를 반환합니다.