zoukankan      html  css  js  c++  java
  • SyncDictionary

        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;
                }
            }
        }
    

      

  • 相关阅读:
    kubernetes 将pod运行在某些特定的节点上,给节点打标签
    kubernetes 安装metrics-server
    数组基本内容,如有遗漏还请指出!
    ES6新属性(追加)
    ES6新属性笔记
    Flex 布局教程
    你知道js中的变量你会定义吗?那常量呢?
    ES6-01:常量与变量的声明
    三种方法实现js中类的继承
    JS完成页面跳转并传参的方法|附加:循环遍历对象
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/8182420.html
Copyright © 2011-2022 走看看