线程安全的List集合(性能比较差..)

由于MS没有提供List的线程安全集合.自己动手写了一个,不过性能…不高..对于性能要求不高的情况下可以使用.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Collections.Concurrent
{
    /// <summary>
    /// 线程安全的List集合
    /// </summary>
    /// <typeparam name="T">类型</typeparam>
    public class ConcurrentList<T> : IList<T>
    {

        /// <summary>
        ///创建一个纯种安全的List集合对像
        /// </summary>
        public ConcurrentList()
        {
            list = new List<T>();
        }

        /// <summary>
        /// 内部的索引器访问对像
        /// </summary>
        protected List<T> list;

        /// <summary>
        /// 锁对像
        /// </summary>
        protected int lockInt;

        #region IList
        /// <summary>
        /// 搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。
        /// </summary>
        /// <param name="item">对像</param>
        /// <returns></returns>
        public int IndexOf(T item)
        {

            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                return list.IndexOf(item);
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }

        }
        /// <summary>
        ///  将元素插入 System.Collections.Generic.List<T> 的指定索引处。
        /// </summary>
        /// <param name="index">指定索引</param>
        /// <param name="item">元素</param>
        public void Insert(int index, T item)
        {

            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                list.Insert(index, item);
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }
        }
        /// <summary>
        /// 移除 System.Collections.Generic.List<T> 的指定索引处的元素。
        /// </summary>
        /// <param name="index">指定索引</param>
        public void RemoveAt(int index)
        {

            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                list.RemoveAt(index);
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }
        }

        public T this[int index]
        {
            get
            {
                System.Threading.Interlocked.Increment(ref lockInt);
                try
                {
                    return list[index];
                }
                finally
                {
                    System.Threading.Interlocked.Decrement(ref lockInt);
                }
            }
            set
            {
                System.Threading.Interlocked.Increment(ref lockInt);
                try
                {
                    list[index] = value;
                }
                finally
                {
                    System.Threading.Interlocked.Decrement(ref lockInt);
                }
            }
        }

        public void Add(T item)
        {

            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                list.Add(item);
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }
        }

        public void Clear()
        {

            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                list.Clear();
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }
        }

        public bool Contains(T item)
        {

            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                return list.Contains(item);
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }
        }

        public void CopyTo(T[] array, int arrayIndex)
        {

            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                list.CopyTo(array, arrayIndex);
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }
        }

        public int Count
        {
            get
            {
                System.Threading.Interlocked.Increment(ref lockInt);
                try
                {
                    return list.Count;
                }
                finally
                {
                    System.Threading.Interlocked.Decrement(ref lockInt);
                }
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }
        /// <summary>
        ///  从 System.Collections.Generic.List<T> 中移除特定对象的第一个匹配项。
        /// </summary>
        /// <param name="item">移除特定对象</param>
        /// <returns></returns>
        public bool Remove(T item)
        {

            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                return list.Remove(item);
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }
        }

        public IEnumerator<T> GetEnumerator()
        {
            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                return list.GetEnumerator();
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        #endregion

        /// <summary>
        /// 提供一个互斥锁
        /// </summary>
        System.Threading.SpinLock splock = new System.Threading.SpinLock();
        /// <summary>
        ///  创建源 System.Collections.Generic.List<T> 中的元素范围的浅表副本。
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="count">总数</param>
        /// <returns></returns>
        public List<T> GetRange(int index, int count)
        {
            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                return list.GetRange(index, count);
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }
        }
        /// <summary>
        ///  从 System.Collections.Generic.List<T> 中移除一定范围的元素。
        /// </summary>
        /// <param name="index">要移除的元素的范围从零开始的起始索引。</param>
        /// <param name="count">要移除的元素数</param>
        public void RemoveRange(int index, int count)
        {
            bool gotLock = false;
            try
            {
                splock.Enter(ref gotLock);
                list.RemoveRange(index, count);
            }
            finally
            {
                if (gotLock)
                {
                    splock.Exit();
                }
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值