Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ตรวจสอบว่า Hashtable ถูกซิงโครไนซ์หรือไม่ใน C#

Hashtable มี property IsSynchronized ที่บอกว่าอินสแตนซ์นั้นเป็น thread-safe (ซิงโครไนซ์) หรือไม่ โดยค่าเริ่มต้น Hashtable ไม่ได้**ถูกซิงโครไนซ์** (IsSynchronized = false) แต่สามารถสร้าง wrapper ที่ thread-safe ได้ด้วย Hashtable.Synchronized()

ตัวอย่างโค้ด: สร้าง Hashtable ธรรมดา และ wrapper ที่ซิงโครไนซ์

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);

        // สร้าง wrapper ที่ thread-safe
        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 ธรรมดา (ไม่ซิงโครไนซ์)

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

สรุป

  • IsSynchronized คืนค่า true ก็ต่อเมื่อเป็น wrapper จาก Hashtable.Synchronized()
  • Hashtable ธรรมดา: IsSynchronized = false, IsFixedSize = false, IsReadOnly = false
  • Wrapper ที่ซิงโครไนซ์: IsSynchronized = true
  • หมายเหตุ: แม้จะซิงโครไนซ์แล้ว การ enum (foreach) ยังไม่ thread-safe ต้อง lock เอง
  • ในโค้ดสมัยใหม่ ควรใช้ ConcurrentDictionary<TKey, TValue> (ใน System.Collections.Concurrent) แทนสำหรับกรณี multi-threaded

รูปแบบการใช้งานที่ถูกต้องกับ Synchronized Hashtable

Hashtable syncHash = Hashtable.Synchronized(new Hashtable());
// สำหรับการ enum ต้อง lock เอง
lock (syncHash.SyncRoot)
{
    foreach (DictionaryEntry entry in syncHash)
    {
        // ...
    }
}