using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Linq; [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
public class SyncDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private Dictionary<TKey, TValue> innerDict;
private ReaderWriterLockSlim readWriteLock; public SyncDictionary()
{
this.readWriteLock = new ReaderWriterLockSlim();
this.innerDict = new Dictionary<TKey, TValue>();
} public SyncDictionary(int capacity)
{
this.readWriteLock = new ReaderWriterLockSlim();
this.innerDict = new Dictionary<TKey, TValue>(capacity);
} public void Add(KeyValuePair<TKey, TValue> item)
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict[item.Key] = item.Value;
}
} public void Add(TKey key, TValue value)
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict[key] = value;
}
} public void Clear()
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict.Clear();
}
} public bool Contains(KeyValuePair<TKey, TValue> item)
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Contains<KeyValuePair<TKey, TValue>>(item);
}
} public bool ContainsKey(TKey key)
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.ContainsKey(key);
}
} public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
using (new AcquireReadLock(this.readWriteLock))
{
this.innerDict.ToArray<KeyValuePair<TKey, TValue>>().CopyTo(array, arrayIndex);
}
} public IEnumerator GetEnumerator()
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.GetEnumerator();
}
} IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.GetEnumerator();
}
} public bool Remove(TKey key)
{
bool isRemoved;
using (new AcquireWriteLock(this.readWriteLock))
{
isRemoved = this.innerDict.Remove(key);
}
return isRemoved;
} public bool Remove(KeyValuePair<TKey, TValue> item)
{
using (new AcquireWriteLock(this.readWriteLock))
{
return this.innerDict.Remove(item.Key);
}
} public bool TryGetValue(TKey key, out TValue value)
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.TryGetValue(key, out value);
}
} public int Count
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Count;
}
}
} public bool IsReadOnly
{
get
{
return false;
}
} public TValue this[TKey key]
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict[key];
}
}
set
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict[key] = value;
}
}
} public ICollection<TKey> Keys
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Keys;
}
}
} public ICollection<TValue> Values
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Values;
}
}
} private class AcquireReadLock : IDisposable
{
private ReaderWriterLockSlim rwLock;
private bool disposedValue; public AcquireReadLock(ReaderWriterLockSlim rwLock)
{
this.rwLock = new ReaderWriterLockSlim();
this.disposedValue = false;
this.rwLock = rwLock;
this.rwLock.EnterReadLock();
} public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue && disposing)
{
this.rwLock.ExitReadLock();
}
this.disposedValue = true;
}
} private class AcquireWriteLock : IDisposable
{
private ReaderWriterLockSlim rwLock;
private bool disposedValue; public AcquireWriteLock(ReaderWriterLockSlim rwLock)
{
this.rwLock = new ReaderWriterLockSlim();
this.disposedValue = false;
this.rwLock = rwLock;
this.rwLock.EnterWriteLock();
} public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue && disposing)
{
this.rwLock.ExitWriteLock();
}
this.disposedValue = true;
}
}
}

  

SyncDictionary的更多相关文章

随机推荐

  1. C# 设置按钮快捷键

    参考自:http://www.csharpwin.com/csharpspace/3932r8132.shtml 一.C# button快捷键之第一种:Alt + *(按钮快捷键) 在Button按钮 ...

  2. spring 线程安全

    http://www.cnblogs.com/doit8791/p/4093808.html 写的真的好

  3. JavaScript使用localStorage缓存Js和css文件

    对于WebApp来说,将js css文件缓存到localstorage区可以减少页面在加载时与HTTP请求的交互次数,从而优化页面的加载时间.特别是当移端信号不好高延迟时优化效果还是很显见的 下面的代 ...

  4. C#使用SmtpClient发送邮件

    目的:写一个可发送邮件的DLL. 原理: 例如A使用163邮箱发送邮件给B(qq邮箱).首先A会把邮件通过SMTP(Simple Mail Transfer Protocol)协议传输到163的Smt ...

  5. 远程获得乐趣的 Linux 命令

    今天是我们为期 24 天的 Linux 命令行玩具日历的最后一天.希望你一直有在看,但如果没有,请从头开始,继续努力.你会发现 Linux 终端有很多游戏.消遣和奇怪之处. 虽然你之前可能已经看过我们 ...

  6. 关于mapreducer 读取hbase数据 存入mysql的实现过程

    mapreducer编程模型是一种八股文的代码逻辑,就以用户行为分析求流存率的作为例子 1.map端来说:必须继承hadoop规定好的mapper类:在读取hbase数据时,已经有现成的接口 Tabl ...

  7. 使用jwt

    jwt:json web token 使用jwt的好处1,无状态2解耦3,更适合于移动端Android,ios4,性能更好5,能够不考虑csrf攻击 ①,客户端请求 Date date = new D ...

  8. JDK源码之HashSet

    1.定义 HashSet继承AbstractSet类,实现Set,Cloneable,Serializable接口.Set 接口是一种不包括重复元素的 Collection,它维持它自己的内部排序,所 ...

  9. Centos 7系统虚拟机桥接模式 固定ip

    前言 本文主要给大家介绍了关于Centos 7系统虚拟机桥接模式的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 主机ping不通虚拟机centos7系统的ip大多有以下原 ...

  10. DDos攻击的常见方法及防御方法

    什么是DDoS? DDoS是英文Distributed Denial of Service的缩写,意即“分布式拒绝服务”,那么什么又是拒绝服务(Denial of Service)呢?可以这么理解,凡 ...