StringDictionary को थ्रेड-सेफ बनाने के लिए SyncRoot प्रॉपर्टी के साथ lock स्टेटमेंट का उपयोग करें।
सिंटैक्स
lock(stringDict.SyncRoot) {
// थ्रेड-सेफ ऑपरेशन्स
}
उदाहरण 1: StringDictionary सिंक्रोनाइज़ेशन
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("A", "Books");
strDict.Add("B", "Electronics");
strDict.Add("C", "Appliances");
strDict.Add("D", "Pet Supplies");
strDict.Add("E", "Clothing");
strDict.Add("F", "Footwear");
Console.WriteLine("StringDictionary key-value pairs...");
foreach (DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
Console.WriteLine("Value associated with key D = " + strDict["D"]);
Console.WriteLine("Value associated with key F = " + strDict["F"]);
Console.WriteLine("\nSynchronize access..");
lock (strDict.SyncRoot) {
foreach (DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
}
आउटपुट
StringDictionary key-value pairs...
a Books
b Electronics
c Appliances
d Pet Supplies
e Clothing
f Footwear
Value associated with key D = Pet Supplies
Value associated with key F = Footwear
Synchronize access..
a Books
b Electronics
c Appliances
d Pet Supplies
e Clothing
f Footwear
उदाहरण 2: अन्य डेटा के साथ
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("A", "John");
strDict.Add("B", "Andy");
strDict.Add("C", "Tim");
strDict.Add("D", "Ryan");
strDict.Add("E", "Kevin");
strDict.Add("F", "Katie");
strDict.Add("G", "Brad");
Console.WriteLine("StringDictionary elements...");
foreach (DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
Console.WriteLine("\nSynchronize access..");
lock (strDict.SyncRoot) {
foreach (DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
}
आउटपुट
StringDictionary elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Synchronize access..
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
महत्वपूर्ण नोट्स
- StringDictionary थ्रेड-सेफ नहीं है डिफ़ॉल्ट रूप से
SyncRootएक ऑब्जेक्ट लौटाता है जिसका उपयोग लॉकिंग के लिए किया जा सकता हैlockस्टेटमेंट यह सुनिश्चित करता है कि एक समय में केवल एक थ्रेड एन्यूमरेट/एक्सेस कर सके- केवल एन्यूमरेशन के दौरान लॉक करना पर्याप्त नहीं - सभी एक्सेस (Add, Remove, Indexer) को लॉक करना चाहिए
आधुनिक विकल्प: ConcurrentDictionary
using System.Collections.Concurrent;
// .NET 4.0+ के लिए अनुशंसित - बिल्ट-इन थ्रेड सेफ्टी
var dict = new ConcurrentDictionary<string, string>();
dict.TryAdd("A", "Books");
dict.TryAdd("B", "Electronics");
// लॉक-मुक्त थ्रेड-सेफ ऑपरेशन्स
string value = dict.GetOrAdd("C", "Appliances");
bool removed = dict.TryRemove("A", out _);
StringDictionary vs ConcurrentDictionary
| विशेषता | StringDictionary + SyncRoot | ConcurrentDictionary |
|---|---|---|
| थ्रेड सेफ्टी | मैन्युअल लॉकिंग आवश्यक | बिल्ट-इन, लॉक-मुक्त |
| पेरफॉरमेंस | लॉक कॉन्टेंशन | हाई कॉनकरेंसी के लिए ऑप्टिमाइज़्ड |
| API | पुराना, नॉन-जेनेरिक | जेनेरिक, आधुनिक |
| एटॉमिक ऑपरेशन्स | नहीं | हाँ (TryAdd, GetOrAdd, आदि) |
सिफारिश
नए कोड के लिए ConcurrentDictionary<TKey, TValue> का उपयोग करें। StringDictionary लिगेसी कोड के लिए है।