How to Create a Thread-Safe Generic List?

后端 未结 8 1216
借酒劲吻你
借酒劲吻你 2020-12-08 10:42

I have a Generic List as below

public static readonly List Customers = new List();

I\'m using the below met

8条回答
  •  盖世英雄少女心
    2020-12-08 11:08

    To expand on @JaradPar's answer, here is a full implementation with a few extra features, as described in the summary

        /// 
    /// a thread-safe list with support for:
    /// 1) negative indexes (read from end).  "myList[-1]" gets the last value
    /// 2) modification while enumerating: enumerates a copy of the collection.
    /// 
    /// 
    public class ConcurrentList : IList
    {
        private object _lock = new object();
        private List _storage = new List();
        /// 
        /// support for negative indexes (read from end).  "myList[-1]" gets the last value
        /// 
        /// 
        /// 
        public TValue this[int index]
        {
            get
            {
                lock (_lock)
                {
                    if (index < 0)
                    {
                        index = this.Count - index;
                    }
                    return _storage[index];
                }
            }
            set
            {
                lock (_lock)
                {
                    if (index < 0)
                    {
                        index = this.Count - index;
                    }
                    _storage[index] = value;
                }
            }
        }
    
        public void Sort()
        {
            lock (_lock)
            {
                _storage.Sort();
            }
        }
    
        public int Count
        {
            get
            {
                return _storage.Count;
            }
        }
    
        bool ICollection.IsReadOnly
        {
            get
            {
                return ((IList)_storage).IsReadOnly;
            }
        }
    
        public void Add(TValue item)
        {
            lock (_lock)
            {
                _storage.Add(item);
            }
        }
    
        public void Clear()
        {
            lock (_lock)
            {
                _storage.Clear();
            }
        }
    
        public bool Contains(TValue item)
        {
            lock (_lock)
            {
                return _storage.Contains(item);
            }
        }
    
        public void CopyTo(TValue[] array, int arrayIndex)
        {
            lock (_lock)
            {
                _storage.CopyTo(array, arrayIndex);
            }
        }
    
    
        public int IndexOf(TValue item)
        {
            lock (_lock)
            {
                return _storage.IndexOf(item);
            }
        }
    
        public void Insert(int index, TValue item)
        {
            lock (_lock)
            {
                _storage.Insert(index, item);
            }
        }
    
        public bool Remove(TValue item)
        {
            lock (_lock)
            {
                return _storage.Remove(item);
            }
        }
    
        public void RemoveAt(int index)
        {
            lock (_lock)
            {
                _storage.RemoveAt(index);
            }
        }
    
        public IEnumerator GetEnumerator()
        {
    
            lock (_lock)
            {
                lock (_lock)
                {
                    return (IEnumerator)_storage.ToArray().GetEnumerator();
                }
            }
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

提交回复
热议问题