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

C#에서 Hashtable이 동기화되어 있는지 확인하는 방법


C#에서 Hashtable이 동기화(synchronized)되어 있는지 확인하려면 IsSynchronized 속성을 사용하면 됩니다. 이 속성은 해당 Hashtable에 대한 액세스가 스레드로부터 안전(thread-safe)하게 동기화되어 있으면 true, 그렇지 않으면 false를 반환하는 불리언 값입니다.

참고로 일반 Hashtable은 기본적으로 동기화되어 있지 않습니다. 동기화된 컬렉션을 만들려면 정적 메서드인 Hashtable.Synchronized()를 호출하여 원본 Hashtable을 감싸는 동기화 래퍼(wrapper)를 생성해야 합니다.

예제

다음은 Hashtable의 동기화 여부를 확인하는 코드입니다 −

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("One", "Katie");
      hash.Add("Two", "John");
      hash.Add("Three", "Barry");
      hash.Add("Four", "");
      hash.Add("Five","Harry");
      hash.Add("Six", "F");
      hash.Add("Seven", "Tom");
      hash.Add("Eight","Andy");
      hash.Add("Nine", "I");
      hash.Add("Ten", "Tim");
      Console.WriteLine("Hashtable Key and Value pairs...");
      foreach(DictionaryEntry entry in hash) {
         Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
      }
      Console.WriteLine("Is the Hashtable having fixed size? = "+hash.IsFixedSize);
      Console.WriteLine("If Hashtable read-only? = "+hash.IsReadOnly);
      Hashtable hash2 = Hashtable.Synchronized(hash);
      Console.WriteLine("Is Hash synchronized = "+hash2.IsSynchronized);
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 출력이 생성됩니다 −

Hashtable Key and Value pairs...
One and Katie
Ten and Tim
Five and Harry
Three and Barry
Seven and Tom
Two and John
Four and  
Eight and Andy
Nine and I
Six and F
Is the Hashtable having fixed size? = False
If Hashtable read-only? = False
Is Hash synchronized = True

위 예제에서는 Hashtable.Synchronized(hash)를 호출하여 동기화된 래퍼 객체인 hash2를 생성했습니다. 따라서 hash2.IsSynchronizedtrue를 반환합니다.

다른 예제

이번에는 동기화되지 않은 일반 Hashtable의 경우를 살펴보겠습니다 −

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("One", 1);
      hash.Add("Two", 2);
      hash.Add("Three", 2);
      hash.Add("Four", 4);
      hash.Add("Five",5);
      hash.Add("Six", 6);
      hash.Add("Seven", 7);
      Console.WriteLine("Hashtable Key and Value pairs...");
      foreach(DictionaryEntry entry in hash) {
         Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
      }
      Console.WriteLine("Is the Hashtable having fixed size? = "+hash.IsFixedSize);
      Console.WriteLine("If Hashtable read-only? = "+hash.IsReadOnly);
      Console.WriteLine("Is Hash synchronized = "+hash.IsSynchronized);
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 출력이 생성됩니다 −

Hashtable Key and Value pairs...
One and 1
Five and 5
Three and 2
Seven and 7
Two and 2
Four and 4
Six and 6
Is the Hashtable having fixed size? = False
If Hashtable read-only? = False
Is Hash synchronized = False

동기화 래퍼 없이 생성한 일반 Hashtable이므로 IsSynchronized 속성이 false를 반환하는 것을 확인할 수 있습니다. 멀티스레드 환경에서 Hashtable을 안전하게 공유해야 한다면 반드시 Hashtable.Synchronized()로 감싸거나, 가능하다면 ConcurrentDictionary<TKey, TValue> 사용을 고려하는 것이 좋습니다.