如题!我以前的时候会这样写
private ArrayList arr= ArrayList.Synchronized(new ArrayList());
现在换成
private List<string> list = new List<string>();
是线程安全的吗?还有
private Hashtable HTTcpCar = Hashtable.Synchronized(new Hashtable());
现在换成
Dictionary<string, ClientState> d = new Dictionary<string, ClientState>();
是线程安全的吗?如果不是,我用泛型的时候怎么实现线程安全?

解决方案 »

  1.   

    在2005年底微软公司正式发布了C# 2.0,与C# 1.x相比,新版本增加了很多新特性,其中最重要的是对泛型的支持。通过泛型,我们可以定义类型安全的数据结构,而无需使用实际的数据类型。这能显著提高性能并得到更高质量的代码。泛型并不是什么新鲜的东西,他在功能上类似于C++的模板,模板多年前就已存在C++上了,并且在C++上有大量成熟应用。
      

  2.   

    1楼,你这段话在网上到处都是,我只关心一个问题,那就是使用泛型是不是线程安全的!
    如果不是,怎么办?
    如果我写成 public class MyArrayList<ItemType> : ArrayList
        {
            ArrayList Items = ArrayList.Synchronized(new ArrayList());        public void Add(ItemType item)
            {
                Items.Add(item);
            }        public void RemoveAt(int index)
            {
                Items.RemoveAt(index);
            }        public ItemType GetItem(int index)
            {
                return (ItemType)Items[index];
            }    } 是否就是线程安全的了呢,如果这样写,是否还具有泛型不拆箱装箱的特性呢?
      

  3.   

     使用lock(ArrayList.SyncRoot )进行同步