C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
2008-04-06 13:50
525 查看
HashTable中的key/value均为object类型,由包含集合元素的存储桶组成。存储桶是HashTable中各元素的虚拟子组,与大多数集合中进行的搜索和检索相比,存储桶可令搜索和检索更为便捷。每一存储桶都与一个哈希代码关联,该哈希代码是使用哈希函数生成的并基于该元素的键。HashTable的优点就在于其索引的方式,速度非常快。如果以任意类型键值访问其中元素会快于其他集合,特别是当数据量特别大的时候,效率差别尤其大。
HashTable的应用场合有:做对象缓存,树递归算法的替代,和各种需提升效率的场合。
Dictionary和HashTable内部实现差不多,但前者无需装箱拆箱操作,效率略高一点。
这样,如果有多个线程并发的企图写HashTable里面的item,则同一时刻只能有一个线程写,其余阻塞;对读的线程则不受影响。
另外一种方法就是使用lock语句,但要lock的不是HashTable,而是其SyncRoot;虽然不推荐这种方法,但效果一样的,因为源代码就是这样实现的:
HashTable的应用场合有:做对象缓存,树递归算法的替代,和各种需提升效率的场合。
//Hashtablesample System.Collections.Hashtableht=newSystem.Collections.Hashtable(); //--Becareful:Keyscan'tbeduplicated,andcan'tbenull---- ht.Add(1,"apple"); ht.Add(2,"banana"); ht.Add(3,"orange"); //Modifyitemvalue: if(ht.ContainsKey(1)) ht[1]="appleBad"; //ThefollowingcodewillreturnnulloValue,noexception objectoValue=ht[5]; //traversal1: foreach(DictionaryEntrydeinht) //traversal2: System.Collections.IDictionaryEnumeratord=ht.GetEnumerator(); while(d.MoveNext()) //Clearitems ht.Clear();
Dictionary和HashTable内部实现差不多,但前者无需装箱拆箱操作,效率略高一点。
//Dictionarysample System.Collections.Generic.Dictionary<int,string>fruits= newSystem.Collections.Generic.Dictionary<int,string>(); fruits.Add(1,"apple"); fruits.Add(2,"banana"); fruits.Add(3,"orange"); foreach(intiinfruits.Keys) if(fruits.ContainsKey(1)) //ArrayList System.Collections.ArrayListlist=newSystem.Collections.ArrayList(); list.Add(1);//objecttype list.Add(2); for(inti=0;i<list.Count;i++) //Hashtablesorting System.Collections.ArrayListakeys=newSystem.Collections.ArrayList(ht.Keys);//fromHashtable akeys.Sort();//Sortbyleadingletter foreach(stringskeyinakeys) //ThreadsafeHashTable System.Collections.HashtablehtSyn=System.Collections.Hashtable.Synchronized(newSystem.Collections.Hashtable());
这样,如果有多个线程并发的企图写HashTable里面的item,则同一时刻只能有一个线程写,其余阻塞;对读的线程则不受影响。
另外一种方法就是使用lock语句,但要lock的不是HashTable,而是其SyncRoot;虽然不推荐这种方法,但效果一样的,因为源代码就是这样实现的:
//Threadsafe
privatestaticSystem.Collections.HashtablehtCache=newSystem.Collections.Hashtable();
publicstaticvoidAccessCache()
{
lock(htCache.SyncRoot)
{
htCache.Add("key","value");
//Becareful:don'tuseforeachtooperationonthewholecollection
//Otherwisethecollectionwon'tbelockedcorrectlyeventhoughindicatedlocked
//--byMSDN
}
}
//Isequivalentto等同于(lockisequivalenttoMonitor.EnterandExit()
publicstaticvoidAccessCache()
{
System.Threading.Monitor.Enter(htCache.SyncRoot);
try
{
/*criticalsection*/
htCache.Add("key","value");
//Becareful:don'tuseforeachtooperationonthewholecollection
//Otherwisethecollectionwon'tbelockedcorrectlyeventhoughindicatedlocked
//--byMSDN
}
finally
{
System.Threading.Monitor.Exit(htCache.SyncRoot);
}
}
相关文章推荐
- C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
- C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
- C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
- C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
- C#集合类(HashTable, Dictionary, ArrayList,List)与HashTable线程安全
- 【转】C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
- [网络收集]C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
- C#常见的几种集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍历方法对比
- 【C#】数组与集合(LIst、ArrayList、HashTable、Dictionary)
- C# 集合类总结(Array,Arraylist,List,Hashtable,Dictionary,Stack,Queue)
- C#中的集合ArrayList、List、Hashtable、Dictionary
- C#集合类(Array、Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)
- C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)
- C#集合类(HashTable, Dictionary, ArrayList)与HashTable线程安全
- C#集合类(Array、Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- 总结C#集合类 Array Arraylist List Hashtable Dictionary
- C#集合类(HashTable, Dictionary, ArrayList)与HashTable
- C# 集合类总结 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- C#使用集合组织数据(HashTable、ArrayList、List<T>,Dictionary<K,V>