DictionarySync和ListSync 线程安全类

 

Dictionary是线程不安全,线程安全的实现类DictionarySync

List是线程不安全,线程安全的实现类ListSync

 

 

 

1、10个线程 每个1000000次读写操作 

      Dictionary用时: 30604 29349 29492

      DictionarySync用时:31343 31054 30265

 

2、hashtable 性能会比Dictionary差点

using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Collections.Generic;


    /// <summary>
    /// 线程安全
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
[Serializable]
public class DictionarySync<TKey, TValue> //: Dictionary<TKey, TValue>
{
    bool _readSync = false;  //读线程安全
    bool _writeSync = false;  //写线程安全
    object _lock = new object();
    Dictionary<TKey, TValue> _mdic = null;

    /// <summary>
    /// 默认 写线程安全
    /// </summary>
    public DictionarySync()
    {
        _writeSync = true;
        if (_mdic == null)
            _mdic = new Dictionary<TKey, TValue>();
    }

    /// <summary>
    /// 线程安全
    /// </summary>
    /// <param name="readSync">读线程安全</param>
    /// <param name="writeSync">写线程安全</param>
    public DictionarySync(bool readSync, bool writeSync)
    {
        _readSync = readSync;
        _writeSync = writeSync;
        if (_mdic == null)
            _mdic = new Dictionary<TKey, TValue>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public void Add(TKey key, TValue value)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Add(key, value);
        }
    }

    /// <summary>
    /// 移除所有键和值
    /// </summary>
    public void Clear()
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Clear();
        }
    }

    /// <summary>
    /// 获取或设置指定的键和值
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public TValue this[TKey key]
    {
        get
        {
            if (_readSync)
            {
                lock (_lock)
                    return _mdic[key];
            }
            else return _mdic[key];
        }
        set
        {
            if (_writeSync)
            {
                lock (_lock)
                    _mdic[key] = value;
            }
            else _mdic[key] = value;
        }
    }

   


    public bool ContainsKey(TKey key)
    {
        if (_readSync)
        {
            lock (_lock)
            return _mdic.ContainsKey(key);
        }
        else return _mdic.ContainsKey(key);
    }

    public bool ContainsValue(TValue value)
    {
        if (_readSync)
        {
            lock (_lock)
            return _mdic.ContainsValue(value);
        }
        else return _mdic.ContainsValue(value);
    }

    public bool Remove(TKey key)
    {
        if (_writeSync)
        {
            lock (_lock)
                return _mdic.Remove(key);
        }
        else return _mdic.Remove(key);

       
    }

 

    public int Count
    {
        get
        {
            if (_readSync)
            {
                lock (_lock)
                    return _mdic.Count;
            }
            else return _mdic.Count;
        }
    }

    public Dictionary<TKey, TValue>.KeyCollection Keys
    {
        get
        {
            if (_readSync)
            {
                lock (_lock)
                    return _mdic.Keys;
            }
            else return _mdic.Keys;
        }
    }

    public Dictionary<TKey, TValue>.ValueCollection Values
    {
        get
        {
            if (_readSync)
            {
                lock (_lock)
                    return _mdic.Values;
            }
            else return _mdic.Values;

        }

    }

 


}


public class ListSync<T>
{
    bool _readSync = false;  //读线程安全
    bool _writeSync = false;  //写线程安全
    object _lock = new object();
    List<T> _mdic = null;

    /// <summary>
    /// 默认 写线程安全
    /// </summary>
    public ListSync()
    {
        _writeSync = true;
        if (_mdic == null)
            _mdic = new List<T>();
    }

    /// <summary>
    /// 线程安全
    /// </summary>
    /// <param name="readSync">读线程安全</param>
    /// <param name="writeSync">写线程安全</param>
    public ListSync(bool readSync, bool writeSync)
    {
        _readSync = readSync;
        _writeSync = writeSync;
        if (_mdic == null)
            _mdic = new List<T>();

    }


    public void Add(T item)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Add(item);
        }
        else _mdic.Add(item);
    }

    public void AddRange(IEnumerable<T> collection)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.AddRange(collection);
        }
        else _mdic.AddRange(collection);
    }

    public void Clear()
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Clear();
        }
        else _mdic.Clear();
    }

    public bool Contains(T item)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.Contains(item);
        }
        else return _mdic.Contains(item);
    }

    public int Count
    {
        get
        {
            if (_readSync)
            {
                lock (_lock)
                    return _mdic.Count;
            }
            else return _mdic.Count;

        }
    }

    public bool Exists(Predicate<T> match)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.Exists(match);
        }
        else return _mdic.Exists(match);
    }


    public T Find(Predicate<T> match)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.Find(match);
        }
        else return _mdic.Find(match);
    }


    public List<T> FindAll(Predicate<T> match)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.FindAll(match);
        }
        else return _mdic.FindAll(match);
    }

    public int FindIndex(Predicate<T> match)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.FindIndex(match);
        }
        else return _mdic.FindIndex(match);
    }
    public int FindIndex(int startIndex, Predicate<T> match)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.FindIndex(startIndex, match);
        }
        else return _mdic.FindIndex(startIndex, match);
    }
    public int FindIndex(int startIndex, int count, Predicate<T> match)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.FindIndex(startIndex, count, match);
        }
        else return _mdic.FindIndex(startIndex, count, match);
    }


    public T FindLast(Predicate<T> match)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.FindLast(match);
        }
        else return _mdic.FindLast(match);
    }

    public int FindLastIndex(Predicate<T> match)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.FindLastIndex(match);
        }
        else return _mdic.FindLastIndex(match);
    }

    public int FindLastIndex(int startIndex, Predicate<T> match)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.FindLastIndex(startIndex, match);
        }
        else return _mdic.FindLastIndex(startIndex, match);
    }

    public int FindLastIndex(int startIndex, int count, Predicate<T> match)
    {

        if (_readSync)
        {
            lock (_lock)
                return _mdic.FindLastIndex(startIndex, count, match);
        }
        else return _mdic.FindLastIndex(startIndex, count, match);
    }


    public int IndexOf(T item)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.IndexOf(item);
        }
        else return _mdic.IndexOf(item);
    }
    public int IndexOf(T item, int index)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.IndexOf(item, index);
        }
        else return _mdic.IndexOf(item, index);
    }
    public int IndexOf(T item, int index, int count)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.IndexOf(item, index, count);
        }
        else return _mdic.IndexOf(item, index, count);
    }
    public void Insert(int index, T item)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Insert(index, item);
        }
        else _mdic.Insert(index, item);
    }
    public void InsertRange(int index, IEnumerable<T> collection)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.InsertRange(index, collection);
        }
        else _mdic.InsertRange(index, collection);
    }

    public bool Remove(T item)
    {
        if (_writeSync)
        {
            lock (_lock)
                return _mdic.Remove(item);
        }
        else return _mdic.Remove(item);
    }
    public int RemoveAll(Predicate<T> match)
    {
        if (_writeSync)
        {
            lock (_lock)
                return _mdic.RemoveAll(match);
        }
        else return _mdic.RemoveAll(match);
    }
    public void RemoveAt(int index)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.RemoveAt(index);
        }
        else _mdic.RemoveAt(index);
    }
    public void RemoveRange(int index, int count)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.RemoveRange(index, count);
        }
        else _mdic.RemoveRange(index, count);
    }
    public void Reverse()
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Reverse();
        }
        else _mdic.Reverse();
    }
    public void Reverse(int index, int count)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Reverse(index, count);
        }
        else _mdic.Reverse(index, count);
    }

    public void Sort()
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Sort();
        }
        else _mdic.Sort();
    }
    public void Sort(Comparison<T> comparison)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Sort(comparison);
        }
        else _mdic.Sort(comparison);
    }
    public void Sort(IComparer<T> comparer)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Sort(comparer);
        }
        else _mdic.Sort(comparer);
    }
    public void Sort(int index, int count, IComparer<T> comparer)
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.Sort(index, count, comparer);
        }
        else _mdic.Sort(index, count, comparer);
    }
    public T[] ToArray()
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.ToArray();
        }
        else return _mdic.ToArray();
    }
    public void TrimExcess()
    {
        if (_writeSync)
        {
            lock (_lock)
                _mdic.TrimExcess();
        }
        else _mdic.TrimExcess();
    }
    public bool TrueForAll(Predicate<T> match)
    {
        if (_readSync)
        {
            lock (_lock)
                return _mdic.TrueForAll(match);
        }
        else return _mdic.TrueForAll(match);
    }

 

 


}

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值