Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C#에서 SortedList 객체가 동기화되어 있는지 확인하는 방법

C#에서 SortedList 객체가 동기화(synchronized)되어 있는지 확인하려면 IsSynchronized 속성을 사용하면 됩니다. 이 속성은 해당 SortedList에 대한 접근이 스레드로부터 안전한지 여부를 bool 값으로 반환합니다.

new 키워드로 생성한 일반 SortedList는 기본적으로 동기화되어 있지 않으므로 IsSynchronized가 false를 반환합니다. 반면 정적 메서드인 SortedList.Synchronized()로 감싼 SortedList는 스레드 안전성이 보장되므로 true를 반환합니다.

예제 1 – Synchronized() 메서드로 동기화된 SortedList 만들기

다음 예제에서는 SortedList를 생성한 뒤 Synchronized() 메서드로 동기화된 래퍼를 만들고, IsSynchronized 속성으로 동기화 여부를 확인합니다.

using System;
using System.Collections;
public class Demo {
    public static void Main() {
        SortedList list = new SortedList();
        list.Add("1", "One");
        list.Add("2", "Two");
        list.Add("3", "Three");
        list.Add("4", "Four");
        list.Add("5", "Five");
        list.Add("6", "Six");
        list.Add("7", "Seven");
        list.Add("8", "Eight");
        Console.WriteLine("Key and Value of SortedList....");
        foreach(DictionaryEntry k in list)
            Console.WriteLine("Key: {0}, Value: {1}", k.Key, k.Value);
        Console.WriteLine("Is the SortedList having the value? " + list.ContainsValue("Five"));
        SortedList sortedList = SortedList.Synchronized(list);
        Console.WriteLine("Is the SortedList synchronized? = " + sortedList.IsSynchronized);
    }
}

출력 결과

Key and Value of SortedList....
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three
Key: 4, Value: Four
Key: 5, Value: Five
Key: 6, Value: Six
Key: 7, Value: Seven
Key: 8, Value: Eight
Is the SortedList having the value? True
Is the SortedList synchronized? = True

ContainsValue("Five")는 값 "Five"가 목록에 존재하므로 true를 반환하며, Synchronized()로 생성한 sortedList의 IsSynchronized 속성 역시 true를 반환하여 동기화가 적용되었음을 확인할 수 있습니다.

예제 2 – 동기화되지 않은 일반 SortedList 확인하기

이번에는 별도의 동기화 처리 없이 생성한 일반 SortedList의 IsSynchronized 속성을 확인해 보겠습니다.

using System;
using System.Collections;
public class Demo {
    public static void Main() {
        SortedList list = new SortedList();
        list.Add("1", "One");
        list.Add("2", "Two");
        list.Add("3", "Three");
        list.Add("4", "Four");
        list.Add("5", "Five");
        list.Add("6", "Six");
        list.Add("7", "Seven");
        list.Add("8", "Eight");
        Console.WriteLine("Key and Value of SortedList....");
        foreach(DictionaryEntry k in list)
            Console.WriteLine("Key: {0}, Value: {1}", k.Key, k.Value);
        Console.WriteLine("Is the SortedList having the value? " + list.ContainsValue("Ten"));
        Console.WriteLine("Is the SortedList synchronized? = " + list.IsSynchronized);
    }
}

출력 결과

Key and Value of SortedList....
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three
Key: 4, Value: Four
Key: 5, Value: Five
Key: 6, Value: Six
Key: 7, Value: Seven
Key: 8, Value: Eight
Is the SortedList having the value? False
Is the SortedList synchronized? = False

값 "Ten"은 목록에 존재하지 않으므로 ContainsValue("Ten")는 false를 반환하고, 동기화 처리를 하지 않은 원본 SortedList의 IsSynchronized 속성 역시 false를 반환합니다.

핵심 정리

  • IsSynchronized – SortedList에 대한 접근이 스레드로부터 안전한지 여부를 나타내는 속성입니다.
  • SortedList.Synchronized() – 지정된 SortedList를 감싸는 스레드 안전(동기화) 래퍼를 반환하는 정적 메서드입니다.
  • 여러 스레드가 동시에 같은 SortedList에 접근하는 환경이라면 반드시 Synchronized()로 감싸서 사용하는 것이 안전합니다.