C#集合类(HashTable, Dictionary, ArrayList,List)与HashTable线程安全
生活随笔
收集整理的這篇文章主要介紹了
C#集合类(HashTable, Dictionary, ArrayList,List)与HashTable线程安全
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
HashTable中的key/value均為object類型,由包含集合元素的存儲桶組成。存儲桶是 HashTable中各元素的虛擬子組,與大多數(shù)集合中進(jìn)行的搜索和檢索相比,存儲桶可令搜索和檢索更為便捷。每一存儲桶都與一個哈希代碼關(guān)聯(lián),該哈希代 碼是使用哈希函數(shù)生成的并基于該元素的鍵。HashTable的優(yōu)點(diǎn)就在于其索引的方式,速度非常快。如果以任意類型鍵值訪問其中元素會快于其他集合,特 別是當(dāng)數(shù)據(jù)量特別大的時候,效率差別尤其大。
HashTable的應(yīng)用場合有:做對象緩存,樹遞歸算法的替代,和各種需提升效率的場合。
???? //Hashtable sample
???? System.Collections.Hashtable ht = new System.Collections.Hashtable();
???? //--Be careful: Keys can't be duplicated, and can't be null----
???? ht.Add(1, "apple");
???? ht.Add(2, "banana");
???? ht.Add(3, "orange");
????
???? //Modify item value:
???? if(ht.ContainsKey(1))
???????? ht[1] = "appleBad";
???? //The following code will return null oValue, no exception
???? object oValue = ht[5];??
????
???? //traversal 1:
???? foreach (DictionaryEntry de in ht)
???? {
???????? Console.WriteLine(de.Key);
???????? Console.WriteLine(de.Value);
???? }
???? //traversal 2:
???? System.Collections.IDictionaryEnumerator d = ht.GetEnumerator();
???? while (d.MoveNext())
???? {
???????? Console.WriteLine("key:{0} value:{1}", d.Entry.Key, d.Entry.Value);
???? }
???? //Clear items
???? ht.Clear();
Dictionary和HashTable內(nèi)部實(shí)現(xiàn)差不多,但前者無需裝箱拆箱操作,效率略高一點(diǎn)。
???? //Dictionary sample
???? System.Collections.Generic.Dictionary<int, string> fruits =
???????? new System.Collections.Generic.Dictionary<int, string>();
???? fruits.Add(1, "apple");
???? fruits.Add(2, "banana");
???? fruits.Add(3, "orange");
???? foreach (int i in fruits.Keys)
???? {
???????? Console.WriteLine("key:{0} value:{1}", i, fruits);
???? }
???? if (fruits.ContainsKey(1))
???? {
???????? Console.WriteLine("contain this key.");
???? }
ArrayList是一維變長數(shù)組,內(nèi)部值為object類型,效率一般:
???? //ArrayList
???? System.Collections.ArrayList list = new System.Collections.ArrayList();
???? list.Add(1);//object type
???? list.Add(2);
???? for (int i = 0; i < list.Count; i++)
???? {
???????? Console.WriteLine(list);
???? }
HashTable是經(jīng)過優(yōu)化的,訪問下標(biāo)的對象先散列過,所以內(nèi)部是無序散列的,保證了高效率,也就是說,其輸出不是按照開始加入的順序,而 Dictionary遍歷輸出的順序,就是加入的順序,這點(diǎn)與Hashtable不同。如果一定要排序HashTable輸出,只能自己實(shí)現(xiàn):
???? //Hashtable sorting
???? System.Collections.ArrayList akeys = new System.Collections.ArrayList(ht.Keys); //from Hashtable
???? akeys.Sort(); //Sort by leading letter
???? foreach (string skey in akeys)
???? {
???????? Console.Write(skey + ":");
???????? Console.WriteLine(ht[skey]);
???? }
??
HashTable與線程安全:
為了保證在多線程的情況下的線程同步訪問安全,微軟提供了自動線程同步的HashTable:
如果 HashTable要允許并發(fā)讀但只能一個線程寫, 要這么創(chuàng)建 HashTable實(shí)例:
???? //Thread safe HashTable
???? System.Collections.Hashtable htSyn = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
這樣, 如果有多個線程并發(fā)的企圖寫HashTable里面的 item, 則同一時刻只能有一個線程寫, 其余阻塞; 對讀的線程則不受影響。
??
另外一種方法就是使用lock語句,但要lock的不是HashTable,而是其SyncRoot;雖然不推薦這種方法,但效果一樣的,因?yàn)樵创a就是這樣實(shí)現(xiàn)的:
???? //Thread safe
???? private static Hashtable htCache = new Hashtable();
???? public static void AccessCache()
???? {
???????? lock (htCache.SyncRoot)
???????? {
???????????? //Do something
???????? }
???? }
HashTable的應(yīng)用場合有:做對象緩存,樹遞歸算法的替代,和各種需提升效率的場合。
???? //Hashtable sample
???? System.Collections.Hashtable ht = new System.Collections.Hashtable();
???? //--Be careful: Keys can't be duplicated, and can't be null----
???? ht.Add(1, "apple");
???? ht.Add(2, "banana");
???? ht.Add(3, "orange");
????
???? //Modify item value:
???? if(ht.ContainsKey(1))
???????? ht[1] = "appleBad";
???? //The following code will return null oValue, no exception
???? object oValue = ht[5];??
????
???? //traversal 1:
???? foreach (DictionaryEntry de in ht)
???? {
???????? Console.WriteLine(de.Key);
???????? Console.WriteLine(de.Value);
???? }
???? //traversal 2:
???? System.Collections.IDictionaryEnumerator d = ht.GetEnumerator();
???? while (d.MoveNext())
???? {
???????? Console.WriteLine("key:{0} value:{1}", d.Entry.Key, d.Entry.Value);
???? }
???? //Clear items
???? ht.Clear();
Dictionary和HashTable內(nèi)部實(shí)現(xiàn)差不多,但前者無需裝箱拆箱操作,效率略高一點(diǎn)。
???? //Dictionary sample
???? System.Collections.Generic.Dictionary<int, string> fruits =
???????? new System.Collections.Generic.Dictionary<int, string>();
???? fruits.Add(1, "apple");
???? fruits.Add(2, "banana");
???? fruits.Add(3, "orange");
???? foreach (int i in fruits.Keys)
???? {
???????? Console.WriteLine("key:{0} value:{1}", i, fruits);
???? }
???? if (fruits.ContainsKey(1))
???? {
???????? Console.WriteLine("contain this key.");
???? }
ArrayList是一維變長數(shù)組,內(nèi)部值為object類型,效率一般:
???? //ArrayList
???? System.Collections.ArrayList list = new System.Collections.ArrayList();
???? list.Add(1);//object type
???? list.Add(2);
???? for (int i = 0; i < list.Count; i++)
???? {
???????? Console.WriteLine(list);
???? }
HashTable是經(jīng)過優(yōu)化的,訪問下標(biāo)的對象先散列過,所以內(nèi)部是無序散列的,保證了高效率,也就是說,其輸出不是按照開始加入的順序,而 Dictionary遍歷輸出的順序,就是加入的順序,這點(diǎn)與Hashtable不同。如果一定要排序HashTable輸出,只能自己實(shí)現(xiàn):
???? //Hashtable sorting
???? System.Collections.ArrayList akeys = new System.Collections.ArrayList(ht.Keys); //from Hashtable
???? akeys.Sort(); //Sort by leading letter
???? foreach (string skey in akeys)
???? {
???????? Console.Write(skey + ":");
???????? Console.WriteLine(ht[skey]);
???? }
??
HashTable與線程安全:
為了保證在多線程的情況下的線程同步訪問安全,微軟提供了自動線程同步的HashTable:
如果 HashTable要允許并發(fā)讀但只能一個線程寫, 要這么創(chuàng)建 HashTable實(shí)例:
???? //Thread safe HashTable
???? System.Collections.Hashtable htSyn = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
這樣, 如果有多個線程并發(fā)的企圖寫HashTable里面的 item, 則同一時刻只能有一個線程寫, 其余阻塞; 對讀的線程則不受影響。
??
另外一種方法就是使用lock語句,但要lock的不是HashTable,而是其SyncRoot;雖然不推薦這種方法,但效果一樣的,因?yàn)樵创a就是這樣實(shí)現(xiàn)的:
???? //Thread safe
???? private static Hashtable htCache = new Hashtable();
???? public static void AccessCache()
???? {
???????? lock (htCache.SyncRoot)
???????? {
???????????? //Do something
???????? }
???? }
轉(zhuǎn)載于:https://www.cnblogs.com/silverLee/archive/2009/11/05/1596774.html
總結(jié)
以上是生活随笔為你收集整理的C#集合类(HashTable, Dictionary, ArrayList,List)与HashTable线程安全的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IT人的十八般武艺-序言
- 下一篇: Windows不同版本的解释